Commits

Bob Swift committed aa98f31

GINT-51: context reporting was not working for all cases. Add abbreviate text helper function.

  • Participants
  • Parent commits 4233687

Comments (0)

Files changed (2)

File src/itest/groovy/helperTest.gant

     }
 )
 
+gint.add(name: 'abbreviateText',
+    inline: {
+        assert helper.abbreviateText('') == ''
+        assert helper.abbreviateText('a') == 'a'
+        assert helper.abbreviateText('abc') == 'abc'
+        assert helper.abbreviateText('abc', 10) == 'abc'
+        assert helper.abbreviateText('abcdefghij', 10) == 'abcdefghij'
+        assert helper.abbreviateText('abcdefghijk', 10) == 'abcdefgijk'
+    }
+)
+
 gint.finalizeTest() // final preparations for running tests

File src/main/groovy/org/swift/tools/Helper.groovy

                     if (!verifySearchNotInTarget(map)) {
                         if (parameters.logFailure == true) {
                             message 'info', 'Found: ' + searchEntry
-                            def index = map?.target?.indexOf(searchEntry)
-                            if (index != null) {
-                                def lower = [0, index - 80].max()
-                                def upper = [
-                                    index + searchEntry.size() + 80,
-                                    map.target.size()
-                                ].min()
-                                message 'info', 'Context: ' + map.target.substring(lower, upper)
+                            if (map?.target instanceof String && searchEntry instanceof String) {
+                                def index = map.target.indexOf(searchEntry)
+                                if (index != null) {
+                                    def lower = [0, index - 80].max()
+                                    def upper = [
+                                        index + searchEntry.size() + 80,
+                                        map.target.size()
+                                    ].min()
+                                    message 'info', 'Context: ' + map.target.substring(lower, upper)
+                                }
                             }
                         }
                         return false // any single failure is a failure for all
             return false
         }
     }
+
+    /**
+     * Generate a text string made up of a subset of the first and last characters of text so that text is no longer than limit
+     * @param text
+     * @param limit or null to return text as is
+     * @param endSize number of end characters used
+     * @return
+     */
+    static public String abbreviateText(final text, int limit = Integer.MAX_VALUE, int endSize = 3) {
+        assert text != null
+        assert endSize > 0
+        assert limit >= endSize // limit must be bigger than the end size
+        return (text.size() < limit) ? text : (text.substring(0, limit - endSize) + text.substring(text.size() - endSize))
+    }
 }