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

New methods copyValueOf, is(Not)Empty and capitalise #323

Open
wants to merge 5 commits 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
80 changes: 78 additions & 2 deletions src/main/java/nl/hsac/fitnesse/fixture/slim/StringFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public String valueOf(String value) {
return value;
}

/**
* NOTE: Duplicate implementation of method 'valueOf' to enable use in scripts started with another fixture containing the same method name (eg. BrowserTest)
* Returns value.
* @param value value to return
*/
public String getValueOf(String value) {
return value;
}

/**
* Determines length of string.
* @param value value to determine length of
Expand All @@ -33,13 +42,60 @@ public int lengthOf(String value) {
return length;
}

/**
* Checks if an input string is empty: null or length() = 0.
*
* @param value value to check.
* @return {@code true} if empty, or {@code false} otherwise.
*/
public boolean isEmpty(String value) { return StringUtils.isEmpty(value); }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really add value on top of a regular Slim regex check (possibly wrapped in a scenario)?


/**
* Checks if a string is not empty: not null and length() != 0.
*
* @param value value to check.
* @return {@code true} if filled (not empty), {@code false} otherwise.
*/
public boolean isNotEmpty(String value) { return !isEmpty(value); }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem


/**
* Checks if a value meets the symbol naming convention ('$xyz').
* This indicates an undefined FitNesse symbol.
* http://fitnesse.org/FitNesse.SuiteAcceptanceTests.SuiteSlimTests.SlimSymbols.NamingConvention
*
* @param value value to check.
* @return {@code true} if undefined (value is symbol name-like), {@code false} otherwise.
*/
public boolean isUndefined(String value) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an unreliable way to check for an undefined symbol. It will also match a string that happens to conform to the regular expression. Is there a better way to check whether a symbol is known in Slim? Maybe it can be checked by having a special Slim table...

If this is a really important thing to check in your tests you could just check this regular expression using standard Slim regex matching and valueOf or getValueOf wrapped in a scenario...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a symbol in your tests that may or may not be assigned a value sounds like a code smell of your tests.

If you worry a symbol may have been assigned a value in a previous test in a suite, just ensure you assign a known (empty, or otherwise not occurring value) in your SetUp.

boolean result = false;
if (getMatcher("\\$[a-zA-Z][a-zA-Z0-9_]*", value).matches()) {
result = true;
}
return result;
}

/**
* Checks if a value is empty or has a value that meets the symbol naming convention ('$xyz').
* The latter indicates an undefined FitNesse symbol.
*
* @param value value to check.
* @return {@code true} if empty or symbol name-like, {@code false} otherwise.
*/
public boolean isEmptyOrUndefined(String value) {
boolean result = false;
if (isEmpty(value) || isUndefined(value)) {
result = true;
}
return result;
}

/**
* <p>Compares two Strings, returning <code>false</code> if they are equal.</p>
*
* <p><code>null</code>s are handled without exceptions. Two <code>null</code>
* references are considered to be equal. The comparison is case sensitive.</p>
*
* @see org.apache.commons.lang3.StringUtils#equals(CharSequence, CharSequence)
* @see StringUtils#equals(CharSequence, CharSequence)
* @param value1 the first String, may be null
* @param value2 the second String, may be null
* @return <code>false</code> if the Strings are equal, or both <code>null</code>
Expand All @@ -54,7 +110,7 @@ public boolean valueDiffersFrom(String value1, String value2) {
* <p><code>null</code>s are handled without exceptions. Two <code>null</code>
* references are considered to be equal. The comparison is case sensitive.</p>
*
* @see org.apache.commons.lang3.StringUtils#equals(CharSequence, CharSequence)
* @see StringUtils#equals(CharSequence, CharSequence)
* @param value1 the first String, may be null
* @param value2 the second String, may be null
* @return <code>true</code> if the Strings are equal, or both <code>null</code>
Expand Down Expand Up @@ -103,6 +159,19 @@ public String convertToLowerCase(String value) {
return result;
}

/**
* Capitalises the first character of a string.
* @param value value to capitalise.
* @return capitalised value.
*/
public String capitalise (String value) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public String capitalise (String value) {
public String capitalise(String value) {

String result = null;
if (value != null) {
result = StringUtils.capitalize(value);
}
return result;
}

/**
* Determines integer value of String (so relative checks can be done).
* @param value string to convert to integer.
Expand Down Expand Up @@ -205,4 +274,11 @@ public String convertLineEndingsToWindows(String input) {
public String convertLineEndingsToUnix(String input) {
return getEnvironment().getLineEndingHelper().convertEndingsTo(input, LineEndingHelper.UNIX_LINE_ENDING);
}

/**
* Returns a null value.
*
* @return null value.
*/
public String setNullValue() { return null;}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public String setNullValue() { return null;}
public String setNullValue() {
return null;
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you use this method in your tests? Is there added value (over just assigning an empty string)?

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,72 @@ public void testLength() {
assertEquals("hello", 5, fixture.lengthOf("hello"));
}

@Test
public void testIsEmpty() {
assertTrue("null", fixture.isEmpty(null));
assertTrue("", fixture.isEmpty(""));
assertFalse(" ", fixture.isEmpty(" "));
assertFalse("hello", fixture.isEmpty("hello"));
assertFalse(" hello ", fixture.isEmpty(" hello "));
}

@Test
public void testIsNotEmpty() {
assertFalse("null", fixture.isNotEmpty(null));
assertFalse("", fixture.isNotEmpty(""));
assertTrue(" ", fixture.isNotEmpty(" "));
assertTrue("hello", fixture.isNotEmpty("hello"));
assertTrue(" hello ", fixture.isNotEmpty(" hello "));
}

@Test
public void testIsUndefined() {
assertTrue("$x", fixture.isUndefined("$x"));
assertTrue("$Y", fixture.isUndefined("$Y"));
assertTrue("$z_", fixture.isUndefined("$z_"));
assertTrue("$xY_z", fixture.isUndefined("$xY_z"));
assertFalse("x$", fixture.isUndefined("x$"));
assertFalse("$1", fixture.isUndefined("$1"));
assertFalse("$_", fixture.isUndefined("$_"));
assertFalse("$$", fixture.isUndefined("$$"));
assertFalse("$x-", fixture.isUndefined("$x-"));
assertFalse("$x.", fixture.isUndefined("$x."));
assertFalse("$x(y)", fixture.isUndefined("$x(y)"));
assertFalse("$1.00-", fixture.isUndefined("$1.00-"));
}

@Test
public void testIsEmptyOrUndefined() {
assertTrue("null", fixture.isEmptyOrUndefined(null));
assertTrue("", fixture.isEmptyOrUndefined(""));
assertTrue("$x", fixture.isEmptyOrUndefined("$x"));
assertTrue("$Y", fixture.isEmptyOrUndefined("$Y"));
assertTrue("$z_", fixture.isEmptyOrUndefined("$z_"));
assertTrue("$xY_z", fixture.isEmptyOrUndefined("$xY_z"));
assertFalse(" ", fixture.isEmptyOrUndefined(" "));
assertFalse("hello", fixture.isEmptyOrUndefined("hello"));
assertFalse(" hello ", fixture.isEmptyOrUndefined(" hello "));
assertFalse("x$", fixture.isEmptyOrUndefined("x$"));
assertFalse("$1", fixture.isEmptyOrUndefined("$1"));
assertFalse("$_", fixture.isEmptyOrUndefined("$_"));
assertFalse("$$", fixture.isEmptyOrUndefined("$$"));
assertFalse("$x-", fixture.isEmptyOrUndefined("$x-"));
assertFalse("$x.", fixture.isEmptyOrUndefined("$x."));
assertFalse("$x(y)", fixture.isEmptyOrUndefined("$x(y)"));
assertFalse("$1.00-", fixture.isEmptyOrUndefined("$1.00-"));
}
@Test
public void testValue() {
assertEquals("null", null, fixture.valueOf(null));
assertNull("null", fixture.valueOf(null));
assertEquals("hello", "hello", fixture.valueOf("hello"));
}

@Test
public void testGetValueOf() {
assertNull("null", fixture.getValueOf(null));
assertEquals("hello", "hello", fixture.getValueOf("hello"));
}

@Test
public void testValueDiffersFrom() {
assertFalse("null - null", fixture.valueDiffersFrom(null, null));
Expand Down Expand Up @@ -100,4 +160,10 @@ public void testConvertToLowerCase() {
assertNull("null", fixture.convertToLowerCase(null));
assertEquals("abC1", "abc1", fixture.convertToLowerCase("abC1"));
}

@Test
public void testCapitalise() {
assertNull("null", fixture.capitalise(null));
assertEquals("abc1", "Abc1", fixture.capitalise("abc1"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,46 @@ It is intended to be used is as a [[library][.FitNesse.UserGuide.WritingAcceptan

This page demonstrates the features of the class (!-nl.hsac.fitnesse.fixture.slim.StringFixture-!) and therefore uses it on its own in a script table (instead of inside a script using a different fixture).

|script|string fixture |
|check |value of |Hello |Hello |
|check |length of |Hello |5 |
|ensure|text |Hello world! |contains |world |
|reject|text |Hello world! |contains |moon |
|ensure|value |Hello |differs from|Hell |
|ensure|value |Hello |equals |Hello |
|check |convert to upper case|Bye |BYE |
|check |convert to lower case|Bye |bye |
|check |remove whitespace |!- Hello World -! |!-HelloWorld-! |
|check |normalize whitespace |!- Hello World -! |Hello World |
|check |replace all |Hello ([A-Za-z]+) |in |Hello World|with|$1 |World |
|check |replace all |(\d{4})-(\d{1,2})-(\d{1,2})|in |1975-12-3 |with|$3-$2-$1|3-12-1975|

|script|string fixture |
|check |value of |Hello |Hello |
|check |length of |Hello |5 |
|ensure|text |Hello world! |contains |world |
|reject|text |Hello world! |contains |moon |
|ensure|value |Hello |differs from|Hell |
|ensure|value |Hello |equals |Hello |
|check |convert to upper case|Bye |BYE |
|check |convert to lower case|Bye |bye |
|check |capitalise |hello world! |Hello world! |
|check |remove whitespace |!- Hello World -! |!-HelloWorld-! |
|check |normalize whitespace |!- Hello World -! |Hello World |
|check |replace all |Hello ([A-Za-z]+) |in |Hello World |with|$1 |World |
|check |replace all |(\d{4})-(\d{1,2})-(\d{1,2})|in |1975-12-3 |with|$3-$2-$1 |3-12-1975 |
|check |replace all |without regex |in |Just replace without regex in a text|with|some text|Just replace some text in a text|

Checks whether a value is stored in a symbol.
* Note that an undefined symbol (i.e. not assigned a value in a fixture method) is NOT empty (null or length=0).
* Note also that you cannot distinquish between a symbol with a 'real' null value and one with the text value 'null'.

|script |string fixture |
|note |symbol $undefined is not used before |
|$realnull=|set null value |
|$textnull=|value of |null |
|$empty= |value of | |
|$filled= |value of |Hello |
|check |is empty |$realnull|true |
|check |is empty |$textnull|false|
|check |is empty |$undefined |false|
|check |is empty |$empty |true |
|check |is not empty |$filled |true |
|check |is undefined |$undefined |true |
|check |is undefined |$empty |false|
|check |is empty or undefined|$undefined |true |
|check |is empty or undefined|$empty |true |
|check |value of |$realnull| |
|check |value of |$textnull| |
|check |value of |$undefined | |
|check |value of |$empty | |
|check |value of |$filled | |

Conversions to numbers (so that [[relative checks][.FitNesse.UserGuide.WritingAcceptanceTests.SliM.ValueComparisons]] can be done).

Expand Down