123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- /*
- * Public Domain Software
- *
- * I (Matthias Ladkau) am the author of the source code in this file.
- * I have placed the source code in this file in the public domain.
- *
- * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
- */
- package parser
- import (
- "testing"
- )
- func TestASTNode(t *testing.T) {
- n, err := ParseWithRuntime("", "- 1", &DummyRuntimeProvider{})
- if err != nil {
- t.Error("Cannot parse test AST:", err)
- return
- }
- n2, err := ParseWithRuntime("", "-2", &DummyRuntimeProvider{})
- if err != nil {
- t.Error("Cannot parse test AST:", err)
- return
- }
- if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: minus > number
- Token is different:
- Pos is different 2 vs 1
- Val is different 1 vs 2
- Lpos is different 3 vs 2
- {
- "ID": 6,
- "Pos": 2,
- "Val": "1",
- "Identifier": false,
- "Lline": 1,
- "Lpos": 3
- }
- vs
- {
- "ID": 6,
- "Pos": 1,
- "Val": "2",
- "Identifier": false,
- "Lline": 1,
- "Lpos": 2
- }
- AST Nodes:
- number: 1
- vs
- number: 2
- ` {
- t.Error("Unexpected result: ", msg)
- return
- }
- n, err = ParseWithRuntime("", "-1", &DummyRuntimeProvider{})
- if err != nil {
- t.Error("Cannot parse test AST:", err)
- return
- }
- n2, err = ParseWithRuntime("", "-a", &DummyRuntimeProvider{})
- if err != nil {
- t.Error("Cannot parse test AST:", err)
- return
- }
- if ok, msg := n.Equals(n2, true); ok || msg != `Path to difference: minus > number
- Name is different number vs identifier
- Token is different:
- ID is different 6 vs 7
- Val is different 1 vs a
- Identifier is different false vs true
- {
- "ID": 6,
- "Pos": 1,
- "Val": "1",
- "Identifier": false,
- "Lline": 1,
- "Lpos": 2
- }
- vs
- {
- "ID": 7,
- "Pos": 1,
- "Val": "a",
- "Identifier": true,
- "Lline": 1,
- "Lpos": 2
- }
- AST Nodes:
- number: 1
- vs
- identifier: a
- ` {
- t.Error("Unexpected result: ", msg)
- return
- }
- n, err = ParseWithRuntime("", "- 1", &DummyRuntimeProvider{})
- if err != nil {
- t.Error("Cannot parse test AST:", err)
- return
- }
- n2, err = ParseWithRuntime("", "a - b", &DummyRuntimeProvider{})
- if err != nil {
- t.Error("Cannot parse test AST:", err)
- return
- }
- if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: minus
- Number of children is different 1 vs 2
- AST Nodes:
- minus
- number: 1
- vs
- minus
- identifier: a
- identifier: b
- ` {
- t.Error("Unexpected result: ", msg)
- return
- }
- n, err = ParseWithRuntime("", "-1 #test", &DummyRuntimeProvider{})
- if err != nil {
- t.Error("Cannot parse test AST:", err)
- return
- }
- n2, err = ParseWithRuntime("", "-1", &DummyRuntimeProvider{})
- if err != nil {
- t.Error("Cannot parse test AST:", err)
- return
- }
- if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: minus > number
- Number of meta data entries is different 1 vs 0
- AST Nodes:
- number: 1 # test
- vs
- number: 1
- ` {
- t.Error("Unexpected result: ", msg)
- return
- }
- n, err = ParseWithRuntime("", "-1 #test", &DummyRuntimeProvider{})
- if err != nil {
- t.Error("Cannot parse test AST:", err)
- return
- }
- n2, err = ParseWithRuntime("", "-1 #wurst", &DummyRuntimeProvider{})
- if err != nil {
- t.Error("Cannot parse test AST:", err)
- return
- }
- if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: minus > number
- Meta data value is different test vs wurst
- AST Nodes:
- number: 1 # test
- vs
- number: 1 # wurst
- ` {
- t.Error("Unexpected result: ", msg)
- return
- }
- n, err = ParseWithRuntime("", "1 #test", &DummyRuntimeProvider{})
- if err != nil {
- t.Error("Cannot parse test AST:", err)
- return
- }
- n2, err = ParseWithRuntime("", "/*test*/ 1", &DummyRuntimeProvider{})
- if err != nil {
- t.Error("Cannot parse test AST:", err)
- return
- }
- if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: number
- Token is different:
- Pos is different 0 vs 9
- Lpos is different 1 vs 10
- {
- "ID": 6,
- "Pos": 0,
- "Val": "1",
- "Identifier": false,
- "Lline": 1,
- "Lpos": 1
- }
- vs
- {
- "ID": 6,
- "Pos": 9,
- "Val": "1",
- "Identifier": false,
- "Lline": 1,
- "Lpos": 10
- }
- Meta data type is different MetaDataPostComment vs MetaDataPreComment
- AST Nodes:
- number: 1 # test
- vs
- number: 1 # test
- ` {
- t.Error("Unexpected result: ", msg)
- return
- }
- // Test building an AST from an invalid
- if _, err := ASTFromJSONObject(map[string]interface{}{
- "value": "foo",
- }); err == nil || err.Error() != "Found json ast node without a name: map[value:foo]" {
- t.Error("Unexpected result: ", err)
- return
- }
- if _, err := ASTFromJSONObject(map[string]interface{}{
- "name": "foo",
- "children": []map[string]interface{}{
- {
- "value": "bar",
- },
- },
- }); err == nil || err.Error() != "Found json ast node without a name: map[value:bar]" {
- t.Error("Unexpected result: ", err)
- return
- }
- // Test population of missing information
- if ast, err := ASTFromJSONObject(map[string]interface{}{
- "name": "foo",
- }); err != nil || ast.String() != "foo\n" || ast.Token.String() != `v:""` {
- t.Error("Unexpected result: ", ast.Token.String(), ast.String(), err)
- return
- }
- if ast, err := ASTFromJSONObject(map[string]interface{}{
- "name": "foo",
- "children": []map[string]interface{}{
- {
- "name": "bar",
- },
- },
- }); err != nil || ast.String() != "foo\n bar\n" || ast.Token.String() != `v:""` {
- t.Error("Unexpected result: ", ast.Token.String(), ast.String(), err)
- return
- }
- }
- func TestLABuffer(t *testing.T) {
- buf := NewLABuffer(Lex("test", "1 2 3 4 5 6 7 8 9"), 3)
- if token, ok := buf.Next(); token.Val != "1" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Next(); token.Val != "2" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- // Check Peek
- if token, ok := buf.Peek(0); token.Val != "3" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Peek(1); token.Val != "4" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Peek(2); token.Val != "5" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Peek(3); token.ID != TokenEOF || ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- // Continue
- if token, ok := buf.Next(); token.Val != "3" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Next(); token.Val != "4" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Next(); token.Val != "5" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Next(); token.Val != "6" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Next(); token.Val != "7" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Next(); token.Val != "8" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- // Check Peek
- if token, ok := buf.Peek(0); token.Val != "9" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Peek(1); token.ID != TokenEOF || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Peek(2); token.ID != TokenEOF || ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- // Continue
- if token, ok := buf.Next(); token.Val != "9" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- // Check Peek
- if token, ok := buf.Peek(0); token.ID != TokenEOF || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Peek(1); token.ID != TokenEOF || ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- // Continue
- if token, ok := buf.Next(); token.ID != TokenEOF || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- // New Buffer
- buf = NewLABuffer(Lex("test", "1 2 3"), 3)
- if token, ok := buf.Next(); token.Val != "1" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Next(); token.Val != "2" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- // Check Peek
- if token, ok := buf.Peek(0); token.Val != "3" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Peek(1); token.ID != TokenEOF || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Peek(2); token.ID != TokenEOF || ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Next(); token.Val != "3" || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Next(); token.ID != TokenEOF || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- // New Buffer - test edge case
- buf = NewLABuffer(Lex("test", ""), 0)
- if token, ok := buf.Peek(0); token.ID != TokenEOF || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Next(); token.ID != TokenEOF || !ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Peek(0); token.ID != TokenEOF || ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- if token, ok := buf.Next(); token.ID != TokenEOF || ok {
- t.Error("Unexpected result: ", token, ok)
- return
- }
- }
|