1// Copyright 2015 Jean Niklas L'orange.  All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package edn
6
7import (
8	"bytes"
9	"testing"
10)
11
12func TestConvert(t *testing.T) {
13	// basic
14	checkConvert(t, "foo bar baz", "foo bar baz")
15	// preserves correct spacing?
16	checkConvert(t, "a,b\nc\td", "a,b\nc\td")
17	// removes unnecessary spacing?
18	checkConvert(t, "a                         b", "a b")
19	// Compacts more complex stuff?
20	checkConvert(t, `{:a "foo", :b zing ,:c 12.3e3}`, `{:a"foo":b zing,:c 12.3e3}`)
21	// Doesn't compact away discards?
22	checkConvert(t, `#_=> nil`, `#_=> nil`)
23	// Removes comments?
24	checkConvert(t, "; just a comment, I am ignored", "")
25	checkConvert(t, "foo;; bar\nbaz", "foo\nbaz")
26	// Doesn't break on delimiters
27	checkConvert(t, "f(x)", "f(x)")
28	checkConvert(t, "#a[1]", "#a[1]")
29	checkConvert(t, "#a #b[1]", "#a #b[1]")
30	checkConvert(t, "#a #b{:x 1}", "#a #b{:x 1}")
31	checkConvert(t, "#tag/a{:x 1}", "#tag/a{:x 1}")
32}
33
34func checkConvert(t *testing.T, input, expected string) {
35	var buf bytes.Buffer
36	err := Compact(&buf, []byte(input))
37	if err != nil {
38		t.Errorf("Unexpected error: %s", err.Error())
39	} else if !bytes.Equal([]byte(expected), buf.Bytes()) {
40		t.Errorf("Convert received '%s', expected '%s' back, was '%s'",
41			input, expected, string(buf.Bytes()))
42	}
43	buf.Reset()
44}
45