Skip to content
This repository was archived by the owner on May 5, 2022. It is now read-only.

Improve type mismatch and unresolved variable exception messages #307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -0,0 +1,22 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* 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.
*/
package com.vaadin.sass.internal.expression.exception;

public class UnresolvedReferenceException extends RuntimeException {
public UnresolvedReferenceException(String errorExpr) {
super(errorExpr);
}
}
27 changes: 20 additions & 7 deletions src/main/java/com/vaadin/sass/internal/parser/LexicalUnitImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import com.vaadin.sass.internal.ScssContext;
import com.vaadin.sass.internal.expression.exception.IncompatibleUnitsException;
import com.vaadin.sass.internal.expression.exception.UnresolvedReferenceException;
import com.vaadin.sass.internal.parser.function.AbsFunctionGenerator;
import com.vaadin.sass.internal.parser.function.AdjustColorFunctionGenerator;
import com.vaadin.sass.internal.parser.function.AlphaFunctionGenerator;
Expand Down Expand Up @@ -401,15 +402,27 @@ public LexicalUnitImpl multiply(LexicalUnitImpl another) {
}

protected short checkAndGetUnit(LexicalUnitImpl another) {
if (getLexicalUnitType() != SAC_INTEGER
&& getLexicalUnitType() != SAC_REAL
&& another.getLexicalUnitType() != SAC_INTEGER
&& another.getLexicalUnitType() != SAC_REAL
// are any variables still unresolved?
if (getLexicalUnitType() == SCSS_VARIABLE) {
// still unresolved
throw new UnresolvedReferenceException("unresolved:" + this);
}
if (another.getLexicalUnitType() == SCSS_VARIABLE) {
// still unresolved
throw new UnresolvedReferenceException("unresolved:" + another);
}
// if the units of the operands are not the same, is at least one of
// them numeric?
boolean isNumeric = getLexicalUnitType() == SAC_INTEGER
|| getLexicalUnitType() == SAC_REAL;
boolean isAnotherNumeric = another.getLexicalUnitType() == SAC_INTEGER
|| another.getLexicalUnitType() == SAC_REAL;
if (!isNumeric && !isAnotherNumeric
&& getLexicalUnitType() != another.getLexicalUnitType()) {
throw new IncompatibleUnitsException(printState());
throw new IncompatibleUnitsException(printState() + " and "
+ another.printState());
}
if (another.getLexicalUnitType() != SAC_INTEGER
&& another.getLexicalUnitType() != SAC_REAL) {
if (!isAnotherNumeric) {
return another.getLexicalUnitType();
}
return getLexicalUnitType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.vaadin.sass.internal.ScssContext;
import com.vaadin.sass.internal.expression.exception.ArithmeticException;
import com.vaadin.sass.internal.expression.exception.IncompatibleUnitsException;
import com.vaadin.sass.internal.expression.exception.UnresolvedReferenceException;
import com.vaadin.sass.internal.parser.LexicalUnitImpl;
import com.vaadin.sass.internal.parser.SassListItem;

Expand Down Expand Up @@ -59,6 +60,8 @@ private LexicalUnitImpl evaluate(SassListItem... terms) {
0, 0);
private final LexicalUnitImpl operatorComma = LexicalUnitImpl.createComma(
2, 3);
private final LexicalUnitImpl unresolved = LexicalUnitImpl.createVariable(
0, 0, "myvar");

@Test
public void testPrecedenceSameAsAppearOrder() {
Expand All @@ -82,6 +85,24 @@ public void testIncompatibleUnit() {
evaluate(operand2cm, operatorMinus, operand3px);
}

@Test
public void testAssumedFirstUnit() {
// 2 - 3px
LexicalUnitImpl result = evaluate(operand2, operatorMinus, operand3px);
Assert.assertEquals(-1, result.getIntegerValue());
Assert.assertEquals(LexicalUnit.SAC_PIXEL, result.getLexicalUnitType());
}

@Test(expected = UnresolvedReferenceException.class)
public void testUnresolvedFirstOperand() {
evaluate(unresolved, operatorMinus, operand2);
}

@Test(expected = UnresolvedReferenceException.class)
public void testUnresolvedSecondOperand() {
evaluate(operand2, operatorMinus, unresolved);
}

@Test
public void testMultiplyWithUnitInfirstOperand() {
// 2cm * 3 = 6cm
Expand Down