@Test public void shouldConvertCamelCaseFieldNameToSnakeCaseParameter() { class Dto { public String someField; @Hint(name = "all_this_was_fields") public String hintedField; } String definition = buckPyFunction.toPythonFunction(BuildRuleType.of("case"), new Dto()); assertEquals( Joiner.on("\n") .join( "@provide_for_build", "def case(name, all_this_was_fields, some_field, visibility=[], build_env=None):", " add_rule({", " 'buck.type' : 'case',", " 'name' : name,", " 'hintedField' : all_this_was_fields,", " 'someField' : some_field,", " 'visibility' : visibility,", " }, build_env)", "", ""), definition); }
@Test public void optionalFieldsAreListedAfterMandatoryOnes() { class Either { // Alphabetical ordering is deliberate. public Optional<String> cat; public String dog; public Optional<String> egg; public String fake; } String definition = buckPyFunction.toPythonFunction(BuildRuleType.of("either"), new Either()); assertEquals( Joiner.on("\n") .join( "@provide_for_build", "def either(name, dog, fake, cat=None, egg=None, visibility=[], build_env=None):", " add_rule({", " 'buck.type' : 'either',", " 'name' : name,", " 'dog' : dog,", " 'fake' : fake,", " 'cat' : cat,", " 'egg' : egg,", " 'visibility' : visibility,", " }, build_env)", "", ""), definition); }
@Test(expected = HumanReadableException.class) public void visibilityOptionsMustNotBeSetAsTheyArePassedInBuildRuleParamsLater() { class Visible { public Set<BuildTargetPattern> visibility; } buckPyFunction.toPythonFunction(BuildRuleType.of("nope"), new Visible()); }
@Test(expected = HumanReadableException.class) public void theNameFieldMustBeAString() { class BadName { public int name; } buckPyFunction.toPythonFunction(BuildRuleType.of("nope"), new BadName()); }
@Test public void visibilityWillBeAddedIfMissing() { class NoVis { public String random; } String definition = buckPyFunction.toPythonFunction(BuildRuleType.of("bad"), new NoVis()); assertTrue(definition.contains("visibility=[]")); }
@Test public void nameWillBeAddedIfMissing() { class NoName { public String random; } String definition = buckPyFunction.toPythonFunction(BuildRuleType.of("bad"), new NoName()); assertTrue(definition.contains("name")); }
@Test public void optionalBooleanValuesShouldBeRepresentedByNone() { class Dto { public Optional<Boolean> field; } String definition = buckPyFunction.toPythonFunction(BuildRuleType.of("boolean"), new Dto()); assertTrue(definition, definition.contains(", field=None,")); }
@Test public void optionalFieldsAreGivenSensibleDefaultValues() { class LotsOfOptions { public Optional<String> thing; public Optional<List<BuildTarget>> targets; public Optional<Integer> version; } String definition = buckPyFunction.toPythonFunction(BuildRuleType.of("optional"), new LotsOfOptions()); assertTrue(definition, definition.contains("targets=[], thing=None, version=None")); }
@Test public void shouldOnlyIncludeTheNameFieldOnce() { class Named { public String name; } String definition = buckPyFunction.toPythonFunction(BuildRuleType.of("named"), new Named()); assertEquals( Joiner.on("\n") .join( "@provide_for_build", "def named(name, visibility=[], build_env=None):", " add_rule({", " 'buck.type' : 'named',", " 'name' : name,", " 'visibility' : visibility,", " }, build_env)", "", ""), definition); }
@Test public void testHasDefaultName() { @TargetName(name = "lollerskates") class NoName { public String foobar; } String definition = buckPyFunction.toPythonFunction(BuildRuleType.of("noname"), new NoName()); assertEquals( Joiner.on("\n") .join( "@provide_for_build", "def noname(foobar, visibility=[], build_env=None):", " add_rule({", " 'buck.type' : 'noname',", " 'name' : 'lollerskates',", " 'foobar' : foobar,", " 'visibility' : visibility,", " }, build_env)", "", ""), definition); }