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

Reactivate tests in org.eclipse.core.expressions.tests #525 #2

Closed
wants to merge 1 commit into from
Closed
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,25 +23,26 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;


/**
* @since 3.7
*/
public class CountExpression extends Expression {

private static final int GREATER_THAN_OR_EQUAL = 9; // [N-
private static final int LESS_THAN_OR_EQUAL = 8; // -N]
private static final int GREATER_THAN = 7; // (N-
private static final int LESS_THAN = 6; // -N)
private static final int ANY_NUMBER = 5; // *
private static final int EXACT = 4; // N
private static final int ONE_OR_MORE = 3; // +
private static final int NONE_OR_ONE = 2; // ?
private static final int NONE = 1; // !
private static final int UNKNOWN = 0;
private static final int LESS_THAN = 6; // -N)
private static final int ANY_NUMBER = 5; // *
private static final int EXACT = 4; // N
private static final int ONE_OR_MORE = 3; // +
private static final int NONE_OR_ONE = 2; // ?
private static final int NONE = 1; // !
private static final int UNKNOWN = 0;

/**
* The seed for the hash code for all count expressions.
*/
private static final int HASH_INITIAL= CountExpression.class.getName().hashCode();
private static final int HASH_INITIAL = CountExpression.class.getName().hashCode();

private int fMode;
private int fSize;
Expand All @@ -62,68 +63,86 @@ public CountExpression(String size) {

private void initializeSize(String size) {
if (size == null)
size= "*"; //$NON-NLS-1$
size = "*"; //$NON-NLS-1$
if ("*".equals(size)) //$NON-NLS-1$
fMode= ANY_NUMBER;
fMode = ANY_NUMBER;
else if ("?".equals(size)) //$NON-NLS-1$
fMode= NONE_OR_ONE;
fMode = NONE_OR_ONE;
else if ("!".equals(size)) //$NON-NLS-1$
fMode= NONE;
fMode = NONE;
else if ("+".equals(size)) //$NON-NLS-1$
fMode= ONE_OR_MORE;
fMode = ONE_OR_MORE;
else if (size.charAt(0) == '-' && size.charAt(size.length() - 1) == ')') {
try {
fMode = LESS_THAN;
fSize = Integer.parseInt(size.substring(1, size.length() - 1));
} catch (NumberFormatException e) {
fMode= UNKNOWN;
fMode = UNKNOWN;
}
} else if (size.charAt(0) == '-' && size.charAt(size.length() - 1) == ']') {
try {
fMode = LESS_THAN_OR_EQUAL;
fSize = Integer.parseInt(size.substring(1, size.length() - 1));
} catch (NumberFormatException e) {
fMode = UNKNOWN;
}
} else if (size.charAt(0) == '(' && size.charAt(size.length() - 1) == '-') {
try {
fMode = GREATER_THAN;
fSize = Integer.parseInt(size.substring(1, size.length() - 1));
} catch (NumberFormatException e) {
fMode= UNKNOWN;
fMode = UNKNOWN;
}
} else if (size.charAt(0) == '[' && size.charAt(size.length() - 1) == '-') {
try {
fMode = GREATER_THAN_OR_EQUAL;
fSize = Integer.parseInt(size.substring(1, size.length() - 1));
} catch (NumberFormatException e) {
fMode = UNKNOWN;
}
} else {
try {
fSize= Integer.parseInt(size);
fMode= EXACT;
fSize = Integer.parseInt(size);
fMode = EXACT;
} catch (NumberFormatException e) {
fMode= UNKNOWN;
fMode = UNKNOWN;
}
}
}

@Override
public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
Object var= context.getDefaultVariable();
Object var = context.getDefaultVariable();
int size;
if (var instanceof Collection) {
size= ((Collection<?>)var).size();
size = ((Collection<?>) var).size();
} else {
ICountable countable= Expressions.getAsICountable(var, this);
ICountable countable = Expressions.getAsICountable(var, this);
if (countable == null)
return EvaluationResult.NOT_LOADED;
size= countable.count();
size = countable.count();
}
switch (fMode) {
case UNKNOWN:
return EvaluationResult.FALSE;
case NONE:
return EvaluationResult.valueOf(size == 0);
case NONE_OR_ONE:
return EvaluationResult.valueOf(size == 0 || size == 1);
case ONE_OR_MORE:
return EvaluationResult.valueOf(size >= 1);
case EXACT:
return EvaluationResult.valueOf(fSize == size);
case ANY_NUMBER:
return EvaluationResult.TRUE;
case LESS_THAN:
return EvaluationResult.valueOf(size < fSize);
case GREATER_THAN:
return EvaluationResult.valueOf(size > fSize);
case UNKNOWN:
return EvaluationResult.FALSE;
case NONE:
return EvaluationResult.valueOf(size == 0);
case NONE_OR_ONE:
return EvaluationResult.valueOf(size == 0 || size == 1);
case ONE_OR_MORE:
return EvaluationResult.valueOf(size >= 1);
case EXACT:
return EvaluationResult.valueOf(fSize == size);
case ANY_NUMBER:
return EvaluationResult.TRUE;
case LESS_THAN:
return EvaluationResult.valueOf(size < fSize);
case GREATER_THAN:
return EvaluationResult.valueOf(size > fSize);
case LESS_THAN_OR_EQUAL:
return EvaluationResult.valueOf(size <= fSize);
case GREATER_THAN_OR_EQUAL:
return EvaluationResult.valueOf(size >= fSize);
}
return EvaluationResult.FALSE;
}
Expand All @@ -138,14 +157,13 @@ public boolean equals(final Object object) {
if (!(object instanceof CountExpression))
return false;

final CountExpression that= (CountExpression)object;
final CountExpression that = (CountExpression) object;
return (this.fMode == that.fMode) && (this.fSize == that.fSize);
}

@Override
protected int computeHashCode() {
return HASH_INITIAL * HASH_FACTOR + fMode
* HASH_FACTOR + fSize;
return HASH_INITIAL * HASH_FACTOR + fMode * HASH_FACTOR + fSize;
}

@Override
Expand All @@ -156,6 +174,12 @@ public String toString() {
builder.append(", mode: "); //$NON-NLS-1$
builder.append(fMode);
switch (fMode) {
case GREATER_THAN_OR_EQUAL:
builder.append(" GREATER_THAN_OR_EQUAL"); //$NON-NLS-1$ [N-
break;
case LESS_THAN_OR_EQUAL:
builder.append(" LESS_THAN_OR_EQUAL"); //$NON-NLS-1$ // -N]
break;
case GREATER_THAN:
builder.append(" GREATER_THAN"); //$NON-NLS-1$
break;
Expand Down Expand Up @@ -186,5 +210,4 @@ public String toString() {
builder.append("]"); //$NON-NLS-1$
return builder.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ public void testAnyNumberExpression() throws CoreException {
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(5)));
}

// @Test
// public void testLessThanOrEqualToExpression() throws CoreException {
// CountExpression e = new CountExpression("-3]"); //$NON-NLS-1$
// Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(1)));
// Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(3)));
// Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(4)));
// }
public void testLessThanOrEqualToExpression() throws CoreException {
CountExpression e = new CountExpression("-3]"); //$NON-NLS-1$
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(1)));
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(3)));
Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(4)));
}

public void testLessThanExpression() throws CoreException {
CountExpression e = new CountExpression("-3)"); //$NON-NLS-1$
Expand All @@ -79,13 +78,12 @@ public void testLessThanExpression() throws CoreException {
Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(4)));
}

// @Test
// public void testGreaterThanOrEqualToExpression() throws CoreException {
// CountExpression e = new CountExpression("[3-"); //$NON-NLS-1$
// Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(5)));
// Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(3)));
// Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(2)));
// }
public void testGreaterThanOrEqualToExpression() throws CoreException {
CountExpression e = new CountExpression("[3-"); //$NON-NLS-1$
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(5)));
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(3)));
Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(2)));
}

public void testGreaterThanExpression() throws CoreException {
CountExpression e = new CountExpression("(3-"); //$NON-NLS-1$
Expand Down