PhpIniParser { public PhpIniFile parse(PhpFile file) { return parse(file.contents()); }  PhpIniFile parse(PhpFile file); PhpIniFile parse(String content);  }
@Test public void no_directive() throws Exception { assertThat(parse("").directives()).isEmpty(); assertThat(parse(" \n ").directives()).isEmpty(); }
@Test public void section() throws Exception { assertThat(parse("[section1]").directives()).isEmpty(); assertThat(parse(" [section1] ").directives()).isEmpty(); checkSingleDirective("[section1]\n name1=value1\n [section2]", "name1", "value1"); }
@Test public void two_directives() throws Exception { List<Directive> directives = parse("name1=value1\nname2=value2").directives(); assertThat(directives).hasSize(2); checkDirective(directives.get(0), "name1", "value1"); checkDirective(directives.get(1), "name2", "value2"); }
@Test public void no_equal_sign() { assertThat(parse("xxx").directives()).isEmpty(); }
@Test public void empty_name() throws Exception { thrown.expect(RecognitionException.class); parse("=value1"); }
@Test public void blank_name() throws Exception { thrown.expect(RecognitionException.class); parse(" =value1"); }
@Test public void more_than_one_equal_sign() throws Exception { thrown.expect(RecognitionException.class); parse("name1=value1=1"); }
@Test public void tokens() throws Exception { Directive directive = parse("\n name1=value1").directives().get(0); checkToken(directive.name(), "name1", 2, 3, 2, 8); checkToken(directive.equalSign(), "=", 2, 8, 2, 9); checkToken(directive.value(), "value1", 2, 9, 2, 15); }
@Test public void parse_file() throws Exception { PhpFile file = FileTestUtils.getFile(new File("src/test/resources/phpini/php.ini")); PhpIniFile phpIni = new PhpIniParser().parse(file); assertThat(phpIni.directives()).hasSize(1); assertThat(phpIni.directives().get(0).name().text()).isEqualTo("max_execution_time"); }
@Test public void unknown_file() throws Exception { PhpIniParser parser = new PhpIniParser(); String fileName = "dir" + File.separator + "xxx.ini"; thrown.expect(IllegalStateException.class); thrown.expectMessage(fileName); PhpFile file = FileTestUtils.getFile(new File(fileName)); parser.parse(file); }
@Test public void directive_by_name() { PhpIniFile phpIniFile = parse("\n name1=value1 \n name2=value2 \n name1=value3"); assertThat(phpIniFile.directivesForName("name1")) .extracting(Directive::value) .extracting(SyntaxToken::text) .containsOnly("value1", "value3"); assertThat(phpIniFile.directivesForName("x")).isEmpty(); }