<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/main_test.go
blob: 1ac89f664e0011aecfce56e4e19203f1b782c55b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main

import (
	"strings"
	"testing"
)

var miscInput string = `{"something":{"nested":"Here is my test value"},"array":["Hello","world","these","are","values"],"people":[{"first_name":"Charlie","last_name":"Johnson","age":22},{"first_name":"Tom","last_name":"Johnson","age":18},{"first_name":"Charlie","last_name":"Chaplin","age":122},{"first_name":"John","last_name":"Johnson","age":48}]}`

func TestMain(t *testing.T) {
	type test struct {
		name string
		program string
		quiet bool
		input string
		expected string
	}

	tests := []test {
		{
			name: "Verbose Extract",
			program: `s/#(~(people)~$_@(1$_#(~(first_name)~$_.|(..$_){-0})-|(..$_){-0})-|(..$_){-0})-/p`,
			quiet: true,
			input: miscInput,
			expected: `"Tom"`,
		},
		{
			name: "Extract",
			program: `s/#("people"$_ @(1 $_#("first_name"$_ .)-)-)-/p`,
			quiet: true,
			input: miscInput,
			expected: `"Tom"`,
		},
		{
			name: "Simple Extract",
			program: "s/#(\"people\" @(1 #(\"first_name\" (.$a))-)-)-$_ `$a`/p",
			quiet: true,
			input: miscInput,
			expected: `"Tom"`,
		},
		{
			name: "Larger Extract",
			program: "s/#(\"people\" @(2 (.$a))-)-$_ `$a`/p",
			quiet: true,
			input: miscInput,
			expected: `{"first_name":"Charlie","last_name":"Chaplin","age":122}`,
		},
		{
			name: "Extract ages",
			program: "s/#(\"people\"$_ :(#(\"age\"$_ .)-):)-/p",
			quiet: true,
			input: miscInput,
			expected: `[22,18,122,48]`,
		},
		{
			name: "Low memory count people",
			program: "aX/#(\"people\" :(#()#):)#$_ `1`/o es/#()#/{ xs/.{-0}+/p }",
			quiet: true,
			input: miscInput,
			expected: "4",
		},
		{
			name: "Get full names",
			program: "s/#(\"people\"$_ .)-/{ s/:():/p as/:(#()#):/{ xdx } s/:(#((\"first_name\" | \"last_name\") .)#)-/X es/@(.#()-)-/{ xs/(#(\"first_name\" \".{-0}$a\")# | #(\"last_name\" \".{-0}$b\")# | .){-0}$_ `\"$a $b\"`/Xxs/-(..)@/p } }",
			quiet: true,
			input: miscInput,
			expected: `["Charlie Johnson","Tom Johnson","Charlie Chaplin","John Johnson"]`,
		},
		{
			name: "Get full names 2",
			program: "s/#(\"people\"$_ .)-/{ s/:():/p as/:(#()#):/{ xdx } X/:(#((\"first_name\" | \"last_name\") .)#)-/o es/@(.#()-)-/{ xX/(#(\"first_name\" \".{-0}$a\")# | #(\"last_name\" \".{-0}$b\")# | .){-0}$_ `\"$a $b\"`/xs/-(..)@/p } }",
			quiet: true,
			input: miscInput,
			expected: `["Charlie Johnson","Tom Johnson","Charlie Chaplin","John Johnson"]`,
		},
		{
			name: "Change full names in place",
			program: "s/#(\"people\" @(. #(\"first_name\" .)#)@)#/{ Nms/#(\"people\" @(. (#(\"first_name\" \".{-0}$a\" \"last_name\" \".{-0}$b\")#$_) `#(\"name\" \"$a $b\")#`)@)#/ }",
			input: miscInput,
			expected: `{"something":{"nested":"Here is my test value"},"array":["Hello","world","these","are","values"],"people":[{"name":"Charlie Johnson","age":22},{"name":"Tom Johnson","age":18},{"name":"Charlie Chaplin","age":122},{"name":"John Johnson","age":48}]}`,
		},
		{
			name: "Get full names with substitute next command",
			program: "s/#( \"people\"$_ :( #( \"first_name\"$_ . )- )- )-/{ N/#( \"people\"$_ :( #( \"last_name\"$_ . )- )- )-/{ s/-( -( ~(.{-0}` `)- ~(.{-0})- )~ ):/p }}",
			quiet: true,
			input: miscInput,
			expected: `["Charlie Johnson","Tom Johnson","Charlie Chaplin","John Johnson"]`,
		},
		{
			name: "Verbose concat array values",
			program: "as/#( \"array\"$_ :(): )-/{ :s N/#( .$_ . )-/{ es/.{-0}:():/be mbs } :em s/:( -( ~(.{-0}` `)-{-0} ~(.{-0})- )~ )-/p }",
			quiet: true,
			input: miscInput,
			expected: `"Hello world these are values"`,
		},
	}

	for _, test := range tests {
		t.Logf("Running test: %s", test.name)

		var output strings.Builder
		run(config {
			quiet: test.quiet,
			program: test.program,
			in: strings.NewReader(test.input),
			out: &output,
		})
		
		if output.String() != test.expected {
			t.Errorf("Ran '%s' and expected %s but got %s", test.program, test.expected, output.String())
		}
	}
}