diff options
| -rw-r--r-- | main/main.go | 64 | ||||
| -rw-r--r-- | main/main_test.go | 40 | 
2 files changed, 80 insertions, 24 deletions
| diff --git a/main/main.go b/main/main.go index b7ef568..3a864ad 100644 --- a/main/main.go +++ b/main/main.go @@ -1,6 +1,7 @@  package main  import ( +	"io"  	"os"  	"bufio"  	"main/walk" @@ -15,32 +16,19 @@ type ProgramState struct {  	pc int  } -func main() { -	quiet := false -	var input string -	hasInput := false -	 -	for i := 1; i < len(os.Args); i += 1 { -		switch os.Args[i] { -			case "-n": -				quiet = true -				continue -		} -		if i < len(os.Args) - 1 { -			panic("Unexpected arguments after program") -		} -		input = os.Args[i] -		hasInput = true -	} -	if !hasInput { -		panic("Missing program") -	} +type config struct { +	quiet bool +	program string +	in io.Reader +	out io.Writer +} -	tokens := Lex(input) +func run(config config) { +	tokens := Lex(config.program)  	program := Parse(tokens) -	stdin := bufio.NewReader(os.Stdin) -	stdout := bufio.NewWriter(os.Stdout) +	stdin := bufio.NewReader(config.in) +	stdout := bufio.NewWriter(config.out)  	state := ProgramState {  		in: json.NewJSONReader(stdin), @@ -58,7 +46,7 @@ func main() {  		for state.pc < len(state.program) {  			state.program[state.pc].exec(&state)  		} -		if !quiet { +		if !config.quiet {  			for _, value := range state.value {  				err := state.out.Write(value)  				if err != nil { @@ -71,3 +59,31 @@ func main() {  	state.in.AssertDone()  	state.out.AssertDone()  } + +func main() { +	config := config { +		quiet: false, +		in: os.Stdin, +		out: os.Stdout, +	} +	hasInput := false +	 +	for i := 1; i < len(os.Args); i += 1 { +		switch os.Args[i] { +			case "-n": +				config.quiet = true +				continue +		} +		if i < len(os.Args) - 1 { +			panic("Unexpected arguments after program") +		} +		config.program = os.Args[i] +		hasInput = true +	} +	if !hasInput { +		panic("Missing program") +	} + +	run(config) + +} diff --git a/main/main_test.go b/main/main_test.go new file mode 100644 index 0000000..a7a7795 --- /dev/null +++ b/main/main_test.go @@ -0,0 +1,40 @@ +package main + +import ( +	"strings" +	"testing" +) + +func TestMain(t *testing.T) { +	type test struct { +		program string +		quiet bool +		input string +		expected string +	} + +	tests := []test { +		{ +			program: `s/#(~(people)~$_@(1$_#(~(first_name)~$_.|(..$_){-0})-|(..$_){-0})-|(..$_){-0})-/p`, +			quiet: true, +			input: `{"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}]}`, +			expected: `"Tom"`, +		}, +	} + +	for i, test := range tests { +		t.Logf("Running test: %d", i) + +		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()) +		} +	} +} | 
