github.com/NeowayLabs/nash@v0.2.2-0.20200127205349-a227041ffd50/ast/node_fmt_test.go (about) 1 package ast 2 3 import ( 4 "testing" 5 6 "github.com/madlambda/nash/token" 7 ) 8 9 func testPrinter(t *testing.T, node Node, expected string) { 10 if node.String() != expected { 11 t.Errorf("Values differ: '%s' != '%s'", node, expected) 12 } 13 } 14 15 func TestAstPrinterStringExpr(t *testing.T) { 16 for _, testcase := range []struct { 17 expected string 18 node Node 19 }{ 20 // quote 21 { 22 expected: `"\""`, 23 node: NewStringExpr(token.NewFileInfo(1, 0), "\"", true), 24 }, 25 26 // escape 27 { 28 expected: `"\\this is a test\n"`, 29 node: NewStringExpr(token.NewFileInfo(1, 0), "\\this is a test\n", true), 30 }, 31 32 // tab 33 { 34 expected: `"this is a test\t"`, 35 node: NewStringExpr(token.NewFileInfo(1, 0), "this is a test\t", true), 36 }, 37 38 // linefeed 39 { 40 expected: `"this is a test\n"`, 41 node: NewStringExpr(token.NewFileInfo(1, 0), "this is a test\n", true), 42 }, 43 { 44 expected: `"\nthis is a test"`, 45 node: NewStringExpr(token.NewFileInfo(1, 0), "\nthis is a test", true), 46 }, 47 { 48 expected: `"\n\n\n"`, 49 node: NewStringExpr(token.NewFileInfo(1, 0), "\n\n\n", true), 50 }, 51 52 // carriege return 53 { 54 expected: `"this is a test\r"`, 55 node: NewStringExpr(token.NewFileInfo(1, 0), "this is a test\r", true), 56 }, 57 { 58 expected: `"\rthis is a test"`, 59 node: NewStringExpr(token.NewFileInfo(1, 0), "\rthis is a test", true), 60 }, 61 { 62 expected: `"\r\r\r"`, 63 node: NewStringExpr(token.NewFileInfo(1, 0), "\r\r\r", true), 64 }, 65 } { 66 testPrinter(t, testcase.node, testcase.expected) 67 } 68 } 69 70 func TestASTPrinterAssignment(t *testing.T) { 71 zeroFileInfo := token.NewFileInfo(1, 0) 72 73 for _, testcase := range []struct { 74 expected string 75 node Node 76 }{ 77 { 78 expected: `a = "1"`, 79 node: NewAssignNode(zeroFileInfo, []*NameNode{ 80 NewNameNode(zeroFileInfo, "a", nil), 81 }, []Expr{NewStringExpr(zeroFileInfo, "1", true)}), 82 }, 83 { 84 expected: `a = ()`, 85 node: NewAssignNode(zeroFileInfo, []*NameNode{ 86 NewNameNode(zeroFileInfo, "a", nil), 87 }, []Expr{NewListExpr(zeroFileInfo, []Expr{})}), 88 }, 89 { 90 expected: `a, b = (), ()`, 91 node: NewAssignNode(zeroFileInfo, []*NameNode{ 92 NewNameNode(zeroFileInfo, "a", nil), 93 NewNameNode(zeroFileInfo, "b", nil), 94 }, []Expr{NewListExpr(zeroFileInfo, []Expr{}), 95 NewListExpr(zeroFileInfo, []Expr{})}), 96 }, 97 { 98 expected: `a, b = "1", "2"`, 99 node: NewAssignNode(zeroFileInfo, []*NameNode{ 100 NewNameNode(zeroFileInfo, "a", nil), 101 NewNameNode(zeroFileInfo, "b", nil), 102 }, []Expr{NewStringExpr(zeroFileInfo, "1", true), 103 NewStringExpr(zeroFileInfo, "2", true)}), 104 }, 105 } { 106 testPrinter(t, testcase.node, testcase.expected) 107 } 108 }