func TestInDir(t *testing.T) { readDir := func(string) ([]os.FileInfo, error) { return []os.FileInfo{ &FileInfo{name: "foo" + goExt, size: 12, isDir: false}, &FileInfo{name: "bar" + fExt, size: int64(len(basicFile)), isDir: false}, &FileInfo{name: "baz", size: 0, isDir: true}, &FileInfo{name: "quux" + fExt, size: 0, isDir: true}, }, nil } p := path.Join("foo", "bar", "baz") files, dirs, err := inDir(p, readDir) defect.Equal(t, err, nil) defect.DeepEqual(t, files, []string{path.Join(p, "bar"+fExt)}) defect.DeepEqual(t, dirs, []string{ path.Join(p, "baz"), path.Join(p, "quux"+fExt), }) }
func TestYyParse(t *testing.T) { fn := func(s string, expected []int) { Output.Mtx.Lock() Output.Val = []int{} p := yyParse(&Lexer{Lexer: util.Lexer{ Reader: util.NewReader(s), IsWhitespace: util.IsWhitespaceST, }}) defect.Equal(t, p, util.EOF) defect.DeepEqual(t, Output.Val, expected) Output.Mtx.Unlock() } fn("6\n", []int{6}) fn("67 + 89\n", []int{67 + 89}) fn("2 * 3\n", []int{6}) fn("6 / 2\n", []int{3}) fn("(4)\n", []int{4}) fn("1 + 3 * (4 / 2 - 5)\n", []int{-8}) fn("a = 1\n", []int{}) fn(`a = 1 a `, []int{1}) fn(`123 + 456 2 - 78 4569 `, []int{123 + 456, 2 - 78, 4569}) fn("1 + 3 * (4 / 2 - 5)\n", []int{-8}) fn(`a = 1 + 3 * (4 / 2 - 5) b = a + 10 b `, []int{2}) // examples from the book fn(`3 * (4 + 5) x = 3 * (4 + 5) y = 5 x y x + 2 * y `, []int{27, 27, 5, 37}) }
func TestV1Parse(t *testing.T) { fn := func(s string, expected []int) { Output.Mtx.Lock() Output.Val = []int{} p := yyParse(&Lexer{Lexer: util.Lexer{ Reader: util.NewReader(s), IsWhitespace: util.IsWhitespaceST, }}) defect.Equal(t, p, util.EOF) defect.DeepEqual(t, Output.Val, expected) Output.Mtx.Unlock() } fn("6\n", []int{6}) fn("67 + 89\n", []int{67 + 89}) fn(`123 + 456 2 - 78 4569 `, []int{123 + 456, 2 - 78, 4569}) }
func TestYyParse(t *testing.T) { fn := func(s string, expected []interface{}) { Mtx.Lock() Initialize() p := yyParse(&Lexer{Lexer: util.Lexer{ Reader: util.NewReader(s), IsWhitespace: util.IsWhitespaceSTNL, }}) defect.Equal(t, p, util.EOF) defect.DeepEqual(t, vals(Output), expected) Mtx.Unlock() } fn("print 6;", []interface{}{6}) fn("print 67 + 89;", []interface{}{67 + 89}) fn("print 2 * 3;", []interface{}{6}) fn("print 6 / 2;", []interface{}{3}) fn("print (4);", []interface{}{4}) fn("print 1 + 3 * (4 / 2 - 5);", []interface{}{-8}) fn("print -2;", []interface{}{-2}) fn("a = 1;", []interface{}{}) fn(`a = 1; print a; `, []interface{}{1}) fn(`print 123 + 456; print 2 - 78; print 4569; `, []interface{}{123 + 456, 2 - 78, 4569}) fn("print 1 + 3 * (4 / 2 - 5);", []interface{}{-8}) fn(`a = 1 + 3 * (4 / 2 - 5); b = a + 10; print b; `, []interface{}{2}) fn("print 1 == 1;", []interface{}{true}) fn("print (1 == 1) == (2 == 3);", []interface{}{false}) fn("print 2 < 1;", []interface{}{false}) fn("print 2 > 1;", []interface{}{true}) fn("print 3 >= 3;", []interface{}{true}) fn("print 4 <= 3;", []interface{}{false}) fn("print 2 != 2;", []interface{}{false}) fn("print (1 != 1) != (1 != 2);", []interface{}{true}) fn("{print 1; print 2;}", []interface{}{1, 2}) fn("{{{print 1;}}}", []interface{}{1}) fn("a = 1; { a = 2; {{ a = 3; a = 4; } a = 5; }} print a;", []interface{}{5}) fn("{a = 1; print a;}", []interface{}{1}) fn("if (1 == 1) {print 1;}", []interface{}{1}) fn("if (1 == 2) {print 1;}", []interface{}{}) fn("a = 1; if (a == 1) { print 1; }", []interface{}{1}) fn("a = 1 == 1; if (a) { print 1; }", []interface{}{1}) fn("{a = 1; if (a == 1) { print 1; }}", []interface{}{1}) fn("a = 1; while (a < 3) { print a; a = a + 1; }", []interface{}{1, 2}) fn("if (1 == 2) print 1; else print 2;", []interface{}{2}) fn("a = 1; if (a == 2) print 1; else print 2;", []interface{}{2}) fn(`a = 1; if (a == 2) { a = a + 1; print a; } else { a = a + 1; if (a == 2) print a == 2; else print a == 2; }`, []interface{}{true}) fn(`a = 1; while (a < 4) { while (a < 4) { print a; a = a + 1; } }`, []interface{}{1, 2, 3}) // examples from the book fn(`print 3 * (4 + 5); x = 3 * (4 + 5); y = 5; print x; print y; print x + 2 * y; `, []interface{}{27, 27, 5, 37}) }