package com.github.jsonj;

import static com.github.jsonj.tools.JsonBuilder.field;
import static com.github.jsonj.tools.JsonBuilder.object;
import static org.assertj.core.api.Assertions.assertThat;

import com.github.jsonj.tools.JsonParser;
import org.testng.annotations.Test;

@Test
public class MapBasedJsonObjectTest {
    private final JsonParser parser = new JsonParser();

    public void shouldBeImmutable() {
        JsonObject o = object(field("x", 42));
        MapBasedJsonObject mo = o.toMapBasedJsonObject();
        assertThat(o.isMutable()).isTrue();
        assertThat(mo.isMutable()).isTrue();
        JsonObject imo = mo.immutableClone();
        assertThat(imo.isMutable()).isFalse();
    }

    public void shouldScaleNumberOfKeys() {
        // this test proves two points:
        // 1) inserting 1000000 elements is fast o(1)
        // 2) establishing a non existent key is not there is fast-ish
        // if it is not, both will take ages
        JsonObject jsonObject = new MapBasedJsonObject();
        for(int i=0;i<100000;i++) {
            jsonObject.put("key_"+i, i);
        }
        JsonElement jsonElement = jsonObject.get("key_IDONTEXIST");
        assertThat(jsonElement).isNull();
        JsonElement parsed = parser.parse(jsonObject.toString());
        assertThat(parsed).isEqualTo(jsonObject);
    }

    public void shouldPreserveNestedStructureWhenUpgradingToMapBasedJsonObject() {
        JsonObject nested=new JsonObject();
        for(int i=0;i<500;i++) {
            nested.put("key_"+i, i);
        }
        JsonObject o = object(field("nested",nested));
        String input=o.toString();
        JsonObject parsed = parser.parseObject(input);
        assertThat(parsed).isEqualTo(o);
    }
}

@@ -1328,32 +1328,388 @@
 Object);%0A    %7D%0A%0A
+    public void shouldParseAsMapBasedJsonObject() %7B%0A        JsonObject jsonObject = new JsonObject();%0A        for(int i=0;i%3C300;i++) %7B%0A            jsonObject.put(%22key_%22+i, i);%0A        %7D%0A        JsonElement parsed = parser.parse(jsonObject.toString());%0A        // because %3E 100 keys%0A        assertThat(parsed).isInstanceOf(MapBasedJsonObject.class);%0A    %7D%0A%0A
     public void 

add test to ensure parser still does the right thing