• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2017 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package ast_test
16
17import (
18	"testing"
19
20	"github.com/google/wuffs/lang/parse"
21
22	t "github.com/google/wuffs/lang/token"
23)
24
25func TestString(tt *testing.T) {
26	const filename = "test.wuffs"
27	testCases := []string{
28		"1",
29		"x",
30
31		"f()",
32		"f(a:i)",
33		"f(a:i, b:j)",
34		"f(a:i, b:j) + 1",
35		"f(a:i, b:j)(c:k)",
36		"f(a:i, b:j)(c:k, d:l, e:m + 2) + 3",
37
38		"x[i]",
39		"x[i][j]",
40		"x[i:j]",
41		"x[i](arg:j)",
42		"x(arg:i)[j]",
43
44		"x.y",
45		"x.y.z.a",
46
47		"+x",
48		"-(x + y)",
49		"not x",
50		"not not x",
51		"+++-x",
52
53		"x + 42",
54		"x and (y < z)",
55		"x & (y as base.u8)",
56		"x * ((a / b) - (i / j))",
57
58		"x + y + z",
59		"x + (i * j.k[l] * (-m << 4) * (n & o(o0:p, o1:q[:r.s + 5]))) + z",
60
61		"x as base.bool",
62		"x as base.u32",
63		"x as T",
64		"x as T",
65		"x as T[i..]",
66		"x as T[..j]",
67		"x as T[i..j]",
68		"x as pkg.T",
69		"x as ptr T",
70		"x as array[4] T",
71		"x as array[8 + (2 * N)] ptr array[4] ptr pkg.T[i..j]",
72	}
73
74	tm := &t.Map{}
75	for _, tc := range testCases {
76		tokens, _, err := t.Tokenize(tm, filename, []byte(tc))
77		if err != nil {
78			tt.Errorf("Tokenize(%q): %v", tc, err)
79			continue
80		}
81		expr, err := parse.ParseExpr(tm, filename, tokens, nil)
82		if err != nil {
83			tt.Errorf("ParseExpr(%q): %v", tc, err)
84			continue
85		}
86		got := expr.Str(tm)
87		if got != tc {
88			tt.Errorf("got %q, want %q", got, tc)
89			continue
90		}
91	}
92}
93