package org.smoothbuild.lang.type;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.smoothbuild.lang.type.ThoroughTypeMatcher.typeMatchingThoroughly;
import static org.testory.Testory.given;
import static org.testory.Testory.thenReturned;
import static org.testory.Testory.when;

import org.junit.Test;
import org.smoothbuild.lang.value.Value;

import com.google.common.collect.ImmutableMap;

public class StructTypeTest extends AbstractTypeTestCase {
  private Value type2;
  private ImmutableMap<String, Type> fields;

  @Override
  protected Type getType(TypeSystem typeSystem) {
    return typeSystem.struct("Struct",
        ImmutableMap.of("a", typeSystem.string(), "b", typeSystem.string()));
  }

  @Test
  public void struct_type_with_different_field_order_has_different_hash() throws Exception {
    given(type = typeSystem.struct("Struct",
        ImmutableMap.of("a", typeSystem.string(), "b", typeSystem.string())));
    given(type2 = typeSystem.struct("Struct",
        ImmutableMap.of("b", typeSystem.string(), "a", typeSystem.string())));
    when(() -> type.hash());
    thenReturned(not(equalTo(type2.hash())));
  }

  @Test
  public void two_level_deep_struct_type_can_be_read_back() throws Exception {
    given(fields = ImmutableMap.of("field1", typeSystem.string(), "field2", typeSystem.string()));
    given(type = typeSystem.struct("TypeName", fields));
    given(fields = ImmutableMap.of("field1", typeSystem.string(), "field2", type));
    given(type = typeSystem.struct("TypeName2", fields));
    when(() -> new TypesDb(hashedDb).read(type.hash()));
    thenReturned(typeMatchingThoroughly(type));
  }
}

@@ -746,32 +746,206 @@
 tring()));%0A  %7D%0A%0A
+  @Test%0A  public void struct_type_without_fields_can_be_created() throws Exception %7B%0A    when(() -%3E typeSystem.struct(%22Struct%22, ImmutableMap.of()));%0A    thenReturned();%0A  %7D%0A%0A
   @Test%0A  public

add StructTypeTest.struct_type_without_fields_can_be_created()