Skip to content

Commit

Permalink
Check for variable referencing order
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsbasjes committed Nov 16, 2017
1 parent 77b4138 commit 8b70419
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Expand Up @@ -229,10 +229,20 @@ public Matcher(Analyzer analyzer,
}
}

// Verify that a variable only contains the variables that have been defined BEFORE it (also not referencing itself).
// If all is ok we link them
Set<MatcherAction> seenVariables = new HashSet<>(variableActions.size());
for (MatcherVariableAction variableAction: variableActions) {
seenVariables.add(variableAction); // Add myself
Set<MatcherAction> interestedActions = informMatcherActionsAboutVariables.get(variableAction.getVariableName());
if (interestedActions != null && !interestedActions.isEmpty()) {
variableAction.setInterestedActions(interestedActions);
for (MatcherAction interestedAction : interestedActions) {
if (seenVariables.contains(interestedAction)) {
throw new InvalidParserConfigurationException("Syntax error: The line >>" + interestedAction + "<< " +
"is referencing variable @"+variableAction.getVariableName()+ " which is not defined yet.");
}
}
}
}

Expand Down Expand Up @@ -399,7 +409,8 @@ public String toString() {
sb.append(" VARIABLE:\n");
for (MatcherAction action : dynamicActions) {
if (action instanceof MatcherVariableAction) {
sb.append(" @").append(((MatcherVariableAction) action).getVariableName()).append(": ").append(action.getMatchExpression()).append("\n");
sb.append(" @").append(((MatcherVariableAction) action).getVariableName())
.append(": ").append(action.getMatchExpression()).append("\n");
sb.append(" -->").append(action.getMatches().toStrings()).append("\n");
}
}
Expand Down
Expand Up @@ -114,6 +114,14 @@ public void checkSyntaxErrorExpect() {
Assert.assertTrue(uaa.runTests(false, false));
}

@Test
public void checkSyntaxErrorVariableBackReference() {
expectedEx.expect(InvalidParserConfigurationException.class);
expectedEx.expectMessage(new StringStartsWith("Syntax error"));

UserAgentAnalyzerTester uaa = new UserAgentAnalyzerTester("classpath*:BadDefinitions/Variable-BackReference.yaml");
Assert.assertTrue(uaa.runTests(false, false));
}

@Test
public void methodInputValidation(){
Expand Down
@@ -0,0 +1,25 @@
#
# Yet Another UserAgent Analyzer
# Copyright (C) 2013-2017 Niels Basjes
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an AS IS BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
config:

- matcher:
variable:
# This must fail because the ordering of the variables is important: First define; then use
- 'VariableOne: NormalizeBrand[@VariableTwo.name]'
- 'VariableTwo: agent.product.version="3"^'
extract:
- 'Dummy : 1: "Dummy"'

0 comments on commit 8b70419

Please sign in to comment.