package eutils

import "testing"

func stringTestTransmute(t *testing.T, name string, proc func(string) string, input, expected string) {

	actual := proc(input)
	if actual != expected {
		t.Errorf("%s(%s) = %s, expected %s", name, input, actual, expected)
	}
}

func TestJSONtoXML(t *testing.T) {

	stringTestTransmute(t, "JSONtoXML,",
		func(str string) string { return JSONtoXML(str, "", "Rec", "") },
		`{"download":"*","collection":"pathway","where":{"ands":[{"geneid":"1956"}]},"order":["taxname,asc"],
		  "start":1,"limit":10000000,"downloadfilename":"GeneID_1956_pathway"}`,
		`<Rec>
  <download>*</download>
  <collection>pathway</collection>
  <where>
    <ands>
      <geneid>1956</geneid>
    </ands>
  </where>
  <order>taxname,asc</order>
  <start>1</start>
  <limit>10000000</limit>
  <downloadfilename>GeneID_1956_pathway</downloadfilename>
</Rec>
`,
	)
}

func TestINItoXML(t *testing.T) {

	stringTestTransmute(t, "INItoXML,",
		func(str string) string { return INItoXML(str) },
		`[section1]
key1 = value1
key2 = value2

[section2]
key3 = value3

[.sub1]
key4 = value4

[section3.sub1]
key5 = value5

[section3.sub2]
key6 = value6
`,
		`<ConfigFile>
  <section1>
    <key1>value1</key1>
    <key2>value2</key2>
  </section1>
  <section2>
    <key3>value3</key3>
    <sub1>
      <key4>value4</key4>
    </sub1>
  </section2>
  <section3>
    <sub1>
      <key5>value5</key5>
    </sub1>
    <sub2>
      <key6>value6</key6>
    </sub2>
  </section3>
</ConfigFile>
`,
	)
}

func TestTOMLtoXML(t *testing.T) {

	stringTestTransmute(t, "TOMLtoXML,",
		func(str string) string { return TOMLtoXML(str) },
		`[section1]
key1 = "value1"
key2 = "value2"

[section2]
key3 = "value3"

[section2.sub1]
key4 = "value4"

[section3.sub1]
key5 = "value5"

[section3.sub2]
key6 = "value6"
`,
		`<ConfigFile>
  <section1>
    <key1>value1</key1>
    <key2>value2</key2>
  </section1>
  <section2>
    <key3>value3</key3>
    <sub1>
      <key4>value4</key4>
    </sub1>
  </section2>
  <section3>
    <sub1>
      <key5>value5</key5>
    </sub1>
    <sub2>
      <key6>value6</key6>
    </sub2>
  </section3>
</ConfigFile>
`,
	)
}

func TestYAMLtoXML(t *testing.T) {

	stringTestTransmute(t, "YAMLtoXML,",
		func(str string) string { return YAMLtoXML(str) },
		`section1:
    key1: value1
    key2: value2
section2:
    key3: value3
`,
		`<ConfigFile>
  <section1>
    <key1>value1</key1>
    <key2>value2</key2>
  </section1>
  <section2>
    <key3>value3</key3>
  </section2>
</ConfigFile>
`,
	)
}
