| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"io/ioutil" |
| 5 |
"os" |
| 6 |
"os/exec" |
| 7 |
"path/filepath" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
var nexBin string |
| 13 |
|
| 14 |
func init() { |
| 15 |
var err error |
| 16 |
if nexBin, err = filepath.Abs(os.Getenv("GOPATH") + "/bin/nex"); err != nil { |
| 17 |
panic(err) |
| 18 |
} |
| 19 |
if _, err := os.Stat(nexBin); err != nil { |
| 20 |
if nexBin, err = filepath.Abs("../nex"); err != nil { |
| 21 |
panic(err) |
| 22 |
} |
| 23 |
if _, err := os.Stat(nexBin); err != nil { |
| 24 |
panic("cannot find nex binary") |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
func dieErr(t *testing.T, err error, s string) { |
| 30 |
if err != nil { |
| 31 |
t.Fatalf("%s: %s", s, err) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func TestWax(t *testing.T) { |
| 36 |
tmpdir, err := ioutil.TempDir("", "nex") |
| 37 |
dieErr(t, err, "TempDir") |
| 38 |
defer func() { |
| 39 |
dieErr(t, os.RemoveAll(tmpdir), "RemoveAll") |
| 40 |
}() |
| 41 |
run := func(s string) { |
| 42 |
v := strings.Split(s, " ") |
| 43 |
err := exec.Command(v[0], v[1:]...).Run() |
| 44 |
dieErr(t, err, s) |
| 45 |
} |
| 46 |
run("cp tacky.nex tacky.y tacky.go build.sh " + tmpdir) |
| 47 |
wd, err := os.Getwd() |
| 48 |
dieErr(t, err, "Getwd") |
| 49 |
dieErr(t, os.Chdir(tmpdir), "Chdir") |
| 50 |
defer func() { |
| 51 |
dieErr(t, os.Chdir(wd), "Chdir") |
| 52 |
}() |
| 53 |
dieErr(t, os.Setenv("NEXBIN", nexBin), "Setenv") |
| 54 |
run("./build.sh") |
| 55 |
cmd := exec.Command("./tacky") |
| 56 |
cmd.Stdin = strings.NewReader(` |
| 57 |
ID10T { |
| 58 |
7 salary = $50000 // Initech salary. |
| 59 |
8a disinterest = // Right-hand side of assignment can span multiple lines. |
| 60 |
$0.01 // Additions are implicit. |
| 61 |
$0.22 |
| 62 |
$3.33 |
| 63 |
21 other income = $305326.13 // Salami slicing. |
| 64 |
22 total income = 7 8a 21 |
| 65 |
40 deduction = [ID10T, TPS Worksheet:10] // Line from another form. |
| 66 |
43 taxable income = 22 - 40 |
| 67 |
44 tax = $123.45 + 43 * 33% - $678.90 // Test precedence, percentages. |
| 68 |
99 = $99.50 |
| 69 |
100 = $100.00 |
| 70 |
101 = $100.49 |
| 71 |
} |
| 72 |
|
| 73 |
ID10T, TPS Worksheet { |
| 74 |
1 bank error = [ID10T:21] |
| 75 |
// Divide by $2500 and round up. |
| 76 |
// Looks wacky because all computations are in cents, but it works. |
| 77 |
// However, in whole dollar mode, any rounding is likely undesirable. |
| 78 |
2bad = (1 + $2500.00 - $0.01) / $2500.00 |
| 79 |
// The following is an example of a formula that is wrongly computed in |
| 80 |
// whole dollar mode. |
| 81 |
2badwrongifrounded = 1 * 2bad * 0.2% |
| 82 |
// The right way: multiply before potential whole dollar rounding. |
| 83 |
2 = 1 * ((1 + $2500.00 - $0.01) / $2500.00) * 0.2% // Fractional percentage. |
| 84 |
// The line '0' is reserved. It always means $0. |
| 85 |
3 = clip(0 - $5) // This should be 0. |
| 86 |
4 = clip($5 - 0) // This should be $5. |
| 87 |
5 = max(3, 4) |
| 88 |
6 = min(5, 2) |
| 89 |
10 = 6 |
| 90 |
} |
| 91 |
|
| 92 |
// Test rounding to dollars. |
| 93 |
whole_dollars_only = 1 |
| 94 |
|
| 95 |
27B-6 { |
| 96 |
99 = [ID10T:99] |
| 97 |
100 = [ID10T:100] |
| 98 |
101 = [ID10T:101] |
| 99 |
} |
| 100 |
`) |
| 101 |
want := `ID10T |
| 102 |
7 salary $50000.00 |
| 103 |
8a disinterest $3.56 |
| 104 |
21 other income $305326.13 |
| 105 |
22 total income $355329.69 |
| 106 |
40 deduction $5.00 |
| 107 |
43 taxable income $355324.69 |
| 108 |
44 tax $116701.70 |
| 109 |
99 $99.50 |
| 110 |
100 $100.00 |
| 111 |
101 $100.49 |
| 112 |
|
| 113 |
ID10T, TPS Worksheet |
| 114 |
1 bank error $305326.13 |
| 115 |
2bad $1.23 |
| 116 |
2badwrongifrounded $75110.23 |
| 117 |
2 $75110.23 |
| 118 |
3 $0.00 |
| 119 |
4 $5.00 |
| 120 |
5 $5.00 |
| 121 |
6 $5.00 |
| 122 |
10 $5.00 |
| 123 |
|
| 124 |
27B-6 |
| 125 |
99 $100.00 |
| 126 |
100 $100.00 |
| 127 |
101 $100.00 |
| 128 |
|
| 129 |
` |
| 130 |
got, err := cmd.CombinedOutput() |
| 131 |
dieErr(t, err, "CombinedOutput") |
| 132 |
if want != string(got) { |
| 133 |
t.Fatalf("want %q, got %q", want, string(got)) |
| 134 |
} |
| 135 |
} |