Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ElementOption class #129

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
*/
package com.github.lombrozo.jsmith.antlr.rules;

import com.github.lombrozo.jsmith.antlr.Context;
import com.github.lombrozo.jsmith.antlr.view.Node;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* ElementOption rule.
* The ANTLR grammar definition:
Expand All @@ -34,13 +40,57 @@
* }
* @since 0.1
*/
public final class ElementOption extends Unimplemented {
public final class ElementOption implements Rule {

/**
* Parent rule.
*/
private final Rule top;

/**
* List of rules.
*/
private final List<Rule> rules;

/**
* Constructor.
* @param parent Parent rule.
*/
public ElementOption(final Rule parent) {
super(parent);
this(parent, new ArrayList<Rule>(0));
}

/**
* Constructor.
* @param parent Parent rule
* @param rules List of rules
*/
public ElementOption(final Rule parent, final List<Rule> rules) {
this.top = parent;
this.rules = rules;
}

@Override
public Rule parent() {
return this.top;
}

@Override
public Node generate(final Context context) throws WrongPathException {
if (this.rules.size() != 1 && this.rules.size() != 3) {
throw new IllegalStateException(
String.format(
"ElementOption should have 1 or 3 rules, provided: %d",
this.rules.size()
)
);
}
return new LeftToRight(this, this.rules).generate(context);
}

@Override
public void append(final Rule rule) {
this.rules.add(rule);
}

@Override
Expand All @@ -50,6 +100,9 @@ public String name() {

@Override
public Rule copy() {
return new ElementOption(this.parent());
return new ElementOption(
this.parent(),
this.rules.stream().map(Rule::copy).collect(Collectors.toList())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* MIT License
*
* Copyright (c) 2023-2025 Volodya Lombrozo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.lombrozo.jsmith.antlr.rules;

import com.github.lombrozo.jsmith.antlr.Context;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Tests for {@link ElementOption}.
*
* @since 0.2.0
*/
final class ElementOptionTest {
@Test
void generatesElementOption() throws WrongPathException {
final ElementOption rule = new ElementOption(new Root());
rule.append(new Identifier(rule, "test"));
rule.append(new Literal("="));
rule.append(new Identifier(rule, "test;"));
MatcherAssert.assertThat(
"We expect ElementOption to generate properly",
rule.generate(new Context()).text().output(),
Matchers.equalTo("test=test;")
);
}

@Test
void throwsIllegalStateException() {
final ElementOption rule = new ElementOption(new Root());
Assertions.assertThrows(
IllegalStateException.class, () -> rule.generate(new Context()),
"We expect it to throw IllegalStateException"
);
}
}
Loading