├── .gitignore ├── .gitmodules ├── .travis.yml ├── README.md ├── build ├── .coveralls.yml ├── ant │ ├── ant-salesforce-33.jar │ └── ant-salesforce-34.jar └── scripts │ ├── combine_tests.sh │ ├── deploy.sh │ ├── run_tests.sh │ ├── setup_credentials.sh │ └── testing │ ├── README.md │ ├── getTestResults.js │ └── package.json └── src ├── classes ├── lo.cls ├── lo.cls-meta.xml ├── lo_test_chunk.cls ├── lo_test_chunk.cls-meta.xml ├── lo_test_drop.cls ├── lo_test_drop.cls-meta.xml ├── lo_test_dropRight.cls ├── lo_test_dropRight.cls-meta.xml ├── lo_test_fill.cls ├── lo_test_fill.cls-meta.xml ├── lo_test_initial.cls ├── lo_test_initial.cls-meta.xml ├── lo_test_last.cls ├── lo_test_last.cls-meta.xml ├── lo_test_pluck.cls ├── lo_test_pluck.cls-meta.xml ├── lo_test_rest.cls ├── lo_test_rest.cls-meta.xml ├── lo_test_slice.cls ├── lo_test_slice.cls-meta.xml ├── lo_test_take.cls ├── lo_test_take.cls-meta.xml ├── lo_test_takeRight.cls ├── lo_test_takeRight.cls-meta.xml ├── lo_test_tostring.cls ├── lo_test_tostring.cls-meta.xml ├── lo_test_union.cls └── lo_test_union.cls-meta.xml └── package.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /config/ 2 | /*sublime-* 3 | /build/scripts/testing/node_modules/ -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "build/solenopsis"] 2 | path = build/solenopsis 3 | url = git://github.com/solenopsis/Solenopsis.git 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: "2.7" 3 | sudo: false 4 | branches: 5 | only: 6 | - master 7 | before_script: ./build/scripts/setup_credentials.sh 8 | script: ./build/scripts/deploy.sh 9 | after_success: ./build/scripts/run_tests.sh -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/apex-lodash/lo.svg?branch=master)](https://travis-ci.org/apex-lodash/lo) 2 | [![Coverage Status](http://coveralls.io/repos/apex-lodash/lo/badge.svg?branch=master&service=github)](http://coveralls.io/github/apex-lodash/lo?branch=master) 3 | 4 | # apex-lodash 5 | This class provides some similar utility methods to that of [lodash](https://lodash.com/). 6 | 7 | # Installation 8 | TODO: Add installation guide 9 | 10 | # Usage 11 | ## Chunk 12 | Takes a given list and breaks it into several list based on a given size 13 | ```java 14 | List myList = new List{ 'a', 'b', 'c', 'd' }; 15 | lo.chunk(myList, 2); 16 | // [['a', 'b'], ['c', 'd']] 17 | lo.chunk(myList, 3); 18 | // [['a', 'b', 'c'], ['d']] 19 | lo.chunk(myList, 10); 20 | // [['a', 'b', 'c', 'd']] 21 | ``` 22 | 23 | ## Drop 24 | Drops a given number of elements off the "left hand side" of a list 25 | 26 | ### Single drop 27 | ```java 28 | List myList = new List{ 'a', 'b', 'c', 'd' }; 29 | lo.drop(myList); 30 | // ['b', 'c', 'd'] 31 | ``` 32 | 33 | ### Provided drop count 34 | ```java 35 | List myList = new List{ 'a', 'b', 'c', 'd' }; 36 | lo.drop(myList, 2); 37 | // ['c', 'd'] 38 | ``` 39 | 40 | ## DropRight 41 | Drops a given number of elements off the "right hand side" of a list 42 | 43 | ### Single drop 44 | ```java 45 | List myList = new List{ 'a', 'b', 'c', 'd' }; 46 | lo.dropRight(myList); 47 | // ['a', 'b', 'c'] 48 | ``` 49 | 50 | ### Provided drop count 51 | ```java 52 | List myList = new List{ 'a', 'b', 'c', 'd' }; 53 | lo.dropRight(myList, 2); 54 | // ['a', 'b'] 55 | ``` 56 | 57 | ## Fill 58 | Fills a list with the given object a given number of times 59 | ```java 60 | lo.fill('foo', 3); 61 | // ['foo', 'foo', 'foo'] 62 | ``` 63 | 64 | ## Initial 65 | Gets all but the last element of a list 66 | ```java 67 | List myList = new List{ 'a', 'b', 'c'}; 68 | lo.initial(myList); 69 | // ['a','b'] 70 | ``` 71 | 72 | ## Last 73 | Gets the last item from a list 74 | ```java 75 | List myList = new List{ 'a', 'b', 'c', 'd' }; 76 | lo.last(myList); 77 | // 'd' 78 | ``` 79 | 80 | ## Pluck 81 | Used to pluck a list of fields out of a list of objects 82 | 83 | ### Single object 84 | ```java 85 | Account account = ...; 86 | Object lo.pluck(account, 'name'); 87 | ``` 88 | 89 | ### Single depth 90 | ```java 91 | List accountList = ...; 92 | List lo.pluck(accountList, 'name'); 93 | ``` 94 | 95 | ### Multiple depths 96 | ```java 97 | List caseList = ...; 98 | List lo.pluck(caseList, 'contact.account.name'); 99 | ``` 100 | 101 | ## Rest 102 | Gets all but the first element of a list 103 | 104 | ```java 105 | List myList = new List{ 'a', 'b', 'c', 'd' }; 106 | lo.rest(myList); 107 | // ['b', 'c', 'd'] 108 | ``` 109 | 110 | ## Slice 111 | Gets part of a list 112 | 113 | ### From given point to end 114 | 115 | ```java 116 | List myList = new List{ 'a', 'b', 'c', 'd', 'e' }; 117 | lo.slice(myList, 2); 118 | // ['c', 'd', 'e'] 119 | ``` 120 | 121 | ### From given point to given point 122 | 123 | ```java 124 | List myList = new List{ 'a', 'b', 'c', 'd', 'e' }; 125 | lo.slice(myList, 2, 4); 126 | // ['c', 'd'] 127 | ``` 128 | 129 | ## Take 130 | Takes a given number of entries off the "left hand side" of a list 131 | 132 | ```java 133 | List myList = new List{ 'a', 'b', 'c', 'd', 'e' }; 134 | lo.take(myList, 2); 135 | // ['a', 'b'] 136 | ``` 137 | 138 | ## TakeRight 139 | Takes a given number of entries off the "right hand side" of a list 140 | 141 | ```java 142 | List myList = new List{ 'a', 'b', 'c', 'd', 'e' }; 143 | lo.takeRight(myList, 2); 144 | // ['d', 'e'] 145 | ``` 146 | 147 | ## ToString 148 | Takes a list of Objects and typecasts them to strings 149 | 150 | ```java 151 | List accountList = ...; 152 | List lo.toString(lo.pluck(accountList, 'name')); 153 | ``` 154 | 155 | # Union 156 | Combines a list of lists of objects and combines them into a single list 157 | 158 | ```java 159 | List> myList = new List>{ 160 | List{'a', 'b'}, 161 | List{'c', 'd'}, 162 | List(), 163 | List{'e'} 164 | }; 165 | lo.union(myList); 166 | // ['a', 'b', 'c', 'd', 'e'] 167 | ``` -------------------------------------------------------------------------------- /build/.coveralls.yml: -------------------------------------------------------------------------------- 1 | repo_token: mnnbnXgJoTfQLlOkNmtITx69PKLJtCOPt -------------------------------------------------------------------------------- /build/ant/ant-salesforce-33.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apex-lodash/lo/b8469d29936e401d6e5b6a7d2e5dc4f2586ccbf0/build/ant/ant-salesforce-33.jar -------------------------------------------------------------------------------- /build/ant/ant-salesforce-34.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apex-lodash/lo/b8469d29936e401d6e5b6a7d2e5dc4f2586ccbf0/build/ant/ant-salesforce-34.jar -------------------------------------------------------------------------------- /build/scripts/combine_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | TEST_PREFIX="lo_test_" 4 | ROOT_DIR="$TRAVIS_BUILD_DIR/src/classes" 5 | TARGET_FILE="$ROOT_DIR/lo_test.cls" 6 | TARGET_FILE_META="$ROOT_DIR/lo_test.cls-meta.xml" 7 | 8 | echo "@isTest" > $TARGET_FILE 9 | echo "private class lo_test {" >> $TARGET_FILE 10 | 11 | for fname in `find $ROOT_DIR -type f -iname "$TEST_PREFIX*.cls"` 12 | do 13 | # NOTE: Last sed does not have any whitespace in front. This should only catch the closing brace. 14 | # This whole class is hacky and should be replaced with a pull via the tooling / metadata api 15 | cat $fname | sed '/^.*@isTest.*$/d' | sed "/^.*private class $TEST_PREFIX.*$/d" | sed '/^}.*$/d' >> $TARGET_FILE 16 | done 17 | 18 | echo "}" >> $TARGET_FILE 19 | 20 | echo '' > $TARGET_FILE_META 21 | echo '' >> $TARGET_FILE_META 22 | echo ' 34.0' >> $TARGET_FILE_META 23 | echo ' Active' >> $TARGET_FILE_META 24 | echo '' >> $TARGET_FILE_META -------------------------------------------------------------------------------- /build/scripts/deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | SOL_ROOT="$TRAVIS_BUILD_DIR/build/solenopsis/scripts" 6 | 7 | cd $SOL_ROOT 8 | ./bsolenopsis push 9 | ./bsolenopsis -Dsf.runAllTests=true run-tests -------------------------------------------------------------------------------- /build/scripts/run_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # If anything fails in the build exit and don't continue on 4 | set -e 5 | 6 | TESTING_ROOT="$TRAVIS_BUILD_DIR/build/scripts/testing" 7 | 8 | cd $TESTING_ROOT 9 | npm cache clean 10 | npm install --save 11 | node getTestResults.js -------------------------------------------------------------------------------- /build/scripts/setup_credentials.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | SOL_PROPFILE="$HOME/solenopsis.properties" 4 | SOL_HOMEDIR="$HOME/.solenopsis" 5 | SOL_CREDDIR="$SOL_HOMEDIR/credentials" 6 | SOL_CREDFILE="$SOL_CREDDIR/lodash.properties" 7 | 8 | SRC_DIR="$TRAVIS_BUILD_DIR/src" 9 | 10 | cat >$SOL_PROPFILE <$SOL_CREDFILE <.cls'); 50 | 51 | console.log('Fetching class information'); 52 | 53 | sfdc_client.tooling.sobject('ApexClass').find({}).execute(function (error, data) { 54 | if (error) { 55 | deferred.reject(new Error(error)); 56 | } else { 57 | console.log('Got information about ' + lo.size(data) + ' classes'); 58 | 59 | lo.forEach(data, function (row) { 60 | if (row.Body.indexOf('@isTest') === -1) { 61 | id_to_class_map[row.Id] = { 62 | name: path_template(row), 63 | source: row.Body, 64 | coverage: [] 65 | }; 66 | } else { 67 | test_class_map[row.Id] = { 68 | name: path_template(row), 69 | source: row.Body 70 | }; 71 | } 72 | }); 73 | 74 | deferred.resolve(); 75 | } 76 | }); 77 | 78 | return deferred.promise; 79 | }; 80 | 81 | /** 82 | * Runs all tests with the tooling api 83 | */ 84 | var runAllTests = function () { 85 | 'use strict'; 86 | 87 | var class_ids = lo.keys(test_class_map), 88 | deferred = Q.defer(); 89 | 90 | sfdc_client.tooling.runTestsAsynchronous(class_ids, function (error, data) { 91 | if (error) { 92 | deferred.reject(new Error(error)); 93 | } else { 94 | deferred.resolve(data); 95 | } 96 | }); 97 | 98 | return deferred.promise; 99 | }; 100 | 101 | /** 102 | * Query the test results 103 | * 104 | * @param testRunId The id of the test run 105 | * @param deferred The Q.defer instance 106 | */ 107 | var queryTestResults = function myself(testRunId, deferred) { 108 | 'use strict'; 109 | 110 | var isComplete = true; 111 | 112 | console.log('Waiting for tests'); 113 | 114 | sfdc_client.query('select Id, Status, ApexClassId from ApexTestQueueItem where ParentJobId = \'' + testRunId + '\'', function (error, data) { 115 | if (error) { 116 | deferred.reject(new Error(error)); 117 | } else { 118 | lo.each(data.records, function (row) { 119 | if (row.Status === 'Queued' || row.Status === 'Processing') { 120 | isComplete = false; 121 | } 122 | }); 123 | 124 | if (isComplete) { 125 | deferred.resolve(); 126 | } else { 127 | myself(testRunId, deferred); 128 | } 129 | } 130 | }); 131 | }; 132 | 133 | /** 134 | * Waits until all tests are completed 135 | * 136 | * @param testRunId The id of the test run 137 | */ 138 | var waitUntilTestsComplete = function (testRunId) { 139 | 'use strict'; 140 | 141 | var deferred = Q.defer(); 142 | 143 | queryTestResults(testRunId, deferred); 144 | 145 | return deferred.promise; 146 | }; 147 | 148 | /** 149 | * Gets the test data and builds an array of the number of times the line was tested 150 | */ 151 | var buildCoverallsCoverage = function () { 152 | 'use strict'; 153 | 154 | var max_line, coverage_size, class_id, i, 155 | deferred = Q.defer(); 156 | 157 | console.log('Fetching code coverage information'); 158 | 159 | sfdc_client.tooling.sobject('ApexCodeCoverage').find({}).execute(function (error, data) { 160 | if (error) { 161 | deferred.reject(new Error(error)); 162 | } else { 163 | console.log('Got information about ' + lo.size(data) + ' tests'); 164 | 165 | 166 | lo.forEach(data, function (row) { 167 | class_id = row.ApexClassOrTriggerId; 168 | 169 | if (lo.has(id_to_class_map, class_id)) { 170 | max_line = lo.max(lo.union(row.Coverage.coveredLines, row.Coverage.uncoveredLines)); 171 | coverage_size = lo.size(id_to_class_map[class_id].coverage); 172 | 173 | if (max_line > coverage_size) { 174 | for (i = coverage_size; i <= max_line; i += 1) { 175 | id_to_class_map[class_id].coverage.push(null); 176 | } 177 | } 178 | 179 | lo.forEach(row.Coverage.coveredLines, function (line_number) { 180 | if (id_to_class_map[class_id].coverage[line_number - 1] === null) { 181 | id_to_class_map[class_id].coverage[line_number - 1] = 1; 182 | } else { 183 | id_to_class_map[class_id].coverage[line_number - 1] += 1; 184 | } 185 | }); 186 | 187 | lo.forEach(row.Coverage.uncoveredLines, function (line_number) { 188 | if (id_to_class_map[class_id].coverage[line_number - 1] === null) { 189 | id_to_class_map[class_id].coverage[line_number - 1] = 0; 190 | } 191 | }); 192 | } 193 | }); 194 | 195 | deferred.resolve(); 196 | } 197 | }); 198 | 199 | return deferred.promise; 200 | }; 201 | 202 | /** 203 | * Posts the data to coveralls 204 | */ 205 | var postToCoveralls = function () { 206 | 'use strict'; 207 | 208 | var fs_stats, post_options, 209 | deferred = Q.defer(), 210 | coveralls_data = { 211 | repo_token: process.env.COVERALLS_REPO_TOKEN, 212 | service_name: 'travis-ci', 213 | service_job_id: process.env.TRAVIS_JOB_ID, 214 | source_files: lo.values(id_to_class_map) 215 | }; 216 | 217 | console.log('Posting data to coveralls'); 218 | 219 | fs.writeFile('/tmp/coveralls_data.json', JSON.stringify(coveralls_data), function (fs_error) { 220 | if (fs_error) { 221 | deferred.reject(new Error(fs_error)); 222 | } else { 223 | fs_stats = fs.statSync('/tmp/coveralls_data.json'); 224 | 225 | post_options = { 226 | multipart: true, 227 | data: { 228 | json_file: restler.file('/tmp/coveralls_data.json', null, fs_stats.size, null, 'application/json') 229 | } 230 | }; 231 | 232 | restler.post('https://coveralls.io/api/v1/jobs', post_options).on("complete", function (data) { 233 | deferred.resolve(); 234 | }); 235 | } 236 | }); 237 | 238 | return deferred.promise; 239 | }; 240 | 241 | Q.fcall(sfdcLogin) 242 | .then(buildClassIdToClassDataMap) 243 | .then(runAllTests) 244 | .then(waitUntilTestsComplete) 245 | .then(buildCoverallsCoverage) 246 | .then(postToCoveralls) 247 | .catch(function (error) { 248 | 'use strict'; 249 | console.log(error); 250 | }) 251 | .done(function () { 252 | 'use strict'; 253 | }); -------------------------------------------------------------------------------- /build/scripts/testing/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "apex-lodash-scripts", 3 | "version": "0.0.1", 4 | "license": "GPL-2.0", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/apex-lodash/lo.git" 8 | }, 9 | "description": "scripts used for building apex-lodash and testing apex-lodash", 10 | "dependencies": { 11 | "jsforce": "jsforce/jsforce", 12 | "q": "*", 13 | "lodash": "*", 14 | "restler": "*" 15 | } 16 | } -------------------------------------------------------------------------------- /src/classes/lo.cls: -------------------------------------------------------------------------------- 1 | public with sharing class lo { 2 | /** Exception denoting if a key is not found in a collection */ 3 | @testVisible private class UnknownKeyException extends Exception {} 4 | 5 | /** Message string for if the key is not part of the collection */ 6 | @testVisible private static final String MSG_UNKNOWN_KEY = 'Unknown key '; 7 | 8 | /** Used to enable / disable logging in the class */ 9 | @testVisible private static Boolean LOGGING = false; 10 | 11 | /** 12 | * If logging for the class is enabled then log the exception 13 | * 14 | * @param e The exception to log 15 | */ 16 | @testVisible 17 | private static void log(Exception e) { 18 | if (LOGGING) { 19 | System.debug(System.LoggingLevel.DEBUG, e.getTypeName() + ': ' + e.getMessage()); 20 | } 21 | } 22 | 23 | /** 24 | * Recursively follows the map down the path 25 | * 26 | * @param collection The map of keys to objects 27 | * @param The path (dot noted) to traverse to get the information 28 | * @return The object at the end of the path 29 | */ 30 | @testVisible 31 | private static Object followPath(Map collection, String path) { 32 | List keys = path.split('\\.', 2); 33 | String key = keys.get(0); 34 | 35 | if (!collection.containsKey(key)) { 36 | throw new UnknownKeyException(MSG_UNKNOWN_KEY + key); 37 | } 38 | 39 | Object data = collection.get(keys.get(0)); 40 | 41 | if (keys.size() == 1) { 42 | return data; 43 | } 44 | 45 | Map nextCollection = (Map)(data); 46 | 47 | return followPath(nextCollection, keys.get(1)); 48 | } 49 | 50 | /** 51 | * Plucks data out of a single object 52 | * 53 | * @param obj The object 54 | * @param path The path (dot noted) to traverse to get the information 55 | */ 56 | public static Object pluck(Object obj, String path) { 57 | String jsonData = JSON.serialize(obj); 58 | Map untypedCollection = (Map)(JSON.deserializeUntyped(jsonData)); 59 | return followPath(untypedCollection, path); 60 | } 61 | 62 | /** 63 | * Plucks data out of a collection of objects 64 | * 65 | * @param collection The list of objects to pull from 66 | * @param path The path (dot noted) to traverse to get the information 67 | */ 68 | public static List pluck(List collection, String path) { 69 | List result = new List(); 70 | List> rawCollection = new List>(); 71 | 72 | for (Object data : collection) { 73 | try { 74 | result.add(pluck(data, path)); 75 | } catch (Exception e) { 76 | log(e); 77 | } 78 | } 79 | 80 | return result; 81 | } 82 | 83 | /** 84 | * A utility method for turning a list of Objects into a list of Strings 85 | * 86 | * @param collection The collection of objects to iterate over 87 | * @return A type casted list of strings 88 | */ 89 | public static List toString(List collection) { 90 | if (collection == null) { 91 | return null; 92 | } 93 | 94 | List result = new List(); 95 | 96 | for (Object obj : collection) { 97 | result.add((String)(obj)); 98 | } 99 | 100 | return result; 101 | } 102 | 103 | /** 104 | * A method to drop a given number of elements off the "left hand side" of an list 105 | * 106 | * @param collection A list of objects to drop from 107 | * @param n The number of items to drop 108 | * @return The reduced collection 109 | */ 110 | public static List drop(List collection, Integer n) { 111 | if (collection == null || n >= collection.size()) { 112 | return new List(); 113 | } 114 | 115 | for (Integer i = 0; i < n; i++) { 116 | collection.remove(0); 117 | } 118 | 119 | return collection; 120 | } 121 | 122 | /** 123 | * A method to drop a single element off the "left hand side" of an list 124 | * 125 | * @param collection A list of objects to drop from 126 | * @return The reduced collection 127 | */ 128 | public static List drop(List collection) { 129 | return drop(collection, 1); 130 | } 131 | 132 | /** 133 | * A method to drop a given number of elements off the "right hand side" of an list 134 | * 135 | * @param collection A list of objects to drop from 136 | * @param n The number of items to drop 137 | * @return The reduced collection 138 | */ 139 | public static List dropRight(List collection, Integer n) { 140 | if (collection == null || n >= collection.size()) { 141 | return new List(); 142 | } 143 | 144 | for (Integer i = 0; i < n; i++) { 145 | collection.remove(collection.size() - 1); 146 | } 147 | 148 | return collection; 149 | } 150 | 151 | /** 152 | * A method to drop a single element off the "right had side" of an list 153 | * 154 | * @param collection A list of objects to drop from 155 | * @return The reduced collection 156 | */ 157 | public static List dropRight(List collection) { 158 | return dropRight(collection, 1); 159 | } 160 | 161 | /** 162 | * A method to generate a filled list of objects 163 | * 164 | * @param source The source object 165 | * @param n The number of times to fill that object 166 | * @return The populated list 167 | */ 168 | public static List fill(Object source, Integer n) { 169 | List result = new List(); 170 | 171 | for (Integer i = 0; i < n; i++) { 172 | result.add(source); 173 | } 174 | 175 | return result; 176 | } 177 | 178 | /** 179 | * A method to take a single list of objects and chunk it into a list of list of objects 180 | * 181 | * @param collection The collection list 182 | * @param n The number of items per chunk 183 | * @return The chunked list 184 | */ 185 | public static List> chunk(List collection, Integer n) { 186 | List collectionCopy = collection.clone(); 187 | List> result = new List>(); 188 | 189 | if (n <= 0) { 190 | return result; 191 | } 192 | 193 | List parts = new List(); 194 | for (Integer i = 1; i < collection.size() + 1; i++) { 195 | parts.add(collectionCopy.remove(0)); 196 | 197 | if (Math.mod(i, n) == 0) { 198 | result.add(parts.clone()); 199 | parts.clear(); 200 | } 201 | } 202 | 203 | if (!parts.isEmpty()) { 204 | result.add(parts); 205 | } 206 | 207 | return result; 208 | } 209 | 210 | /** 211 | * A method to get the last element of the list 212 | * 213 | * @param collection The collection list 214 | * @return The last element in the list 215 | */ 216 | public static Object last(List collection) { 217 | if (collection == null || collection.isEmpty()) { 218 | return null; 219 | } 220 | 221 | return collection.get(collection.size() - 1); 222 | } 223 | 224 | /** 225 | * Gets all of the list except for the first element 226 | * 227 | * @param collection The collection list 228 | * @return All of the list except the first element 229 | */ 230 | public static List rest(List collection) { 231 | if (collection == null || collection.isEmpty()) { 232 | return new List(); 233 | } 234 | 235 | List collectionCopy = collection.clone(); 236 | collectionCopy.remove(0); 237 | 238 | return collectionCopy; 239 | } 240 | 241 | /** 242 | * Slices out parts of an list from the given start to the given end 243 | * 244 | * @param collection The collection list 245 | * @param start The starting index 246 | * @param stop The ending index 247 | * @return The new list 248 | */ 249 | public static List slice(List collection, Integer start, Integer stop) { 250 | if (collection == null || collection.isEmpty() || start > stop) { 251 | return new List(); 252 | } 253 | 254 | List collectionCopy = collection.clone(); 255 | List result = new List(); 256 | 257 | start = (start < 0) ? 0 : start; 258 | stop = (stop > collectionCopy.size()) ? collectionCopy.size() : stop; 259 | 260 | for (Integer i = start; i < stop; i++) { 261 | result.add(collectionCopy.remove(start)); 262 | } 263 | 264 | return result; 265 | } 266 | 267 | /** 268 | * Slices from the given index to the end of the list 269 | * 270 | * @param collection The collection list 271 | * @param start The starting index 272 | * @return The new list 273 | */ 274 | public static List slice(List collection, Integer start) { 275 | if (collection == null || collection.isEmpty()) { 276 | return new List(); 277 | } 278 | 279 | return slice(collection, start, collection.size()); 280 | } 281 | 282 | /** 283 | * Takes a list of lists and makes them into a single list 284 | * 285 | * @param collection The list of lists 286 | * @return The new list 287 | */ 288 | public static List union(List> collection) { 289 | List result = new List(); 290 | 291 | if (collection == null || collection.isEmpty()) { 292 | return result; 293 | } 294 | 295 | List> clonedCollection = collection.clone(); 296 | 297 | for (List subCollection : clonedCollection) { 298 | if (subCollection == null || subCollection.isEmpty()) { 299 | continue; 300 | } 301 | 302 | Integer subCollectionSize = subCollection.size(); 303 | 304 | for (Integer i = 0; i < subCollectionSize; i++) { 305 | result.add(subCollection.remove(0)); 306 | } 307 | } 308 | 309 | return result; 310 | } 311 | 312 | /** 313 | * Generates a list of parts from the "left hand side" of the list 314 | * 315 | * @param collection The list of objects 316 | * @param n The number of parts to return 317 | * @return The new list 318 | */ 319 | public static List take(List collection, Integer n) { 320 | return slice(collection, 0, n); 321 | } 322 | 323 | /** 324 | * Generates a list of parts from the "right hand side" of the list 325 | * 326 | * @param collection The list of objects 327 | * @param n The number of parts to return 328 | * @return The new list 329 | */ 330 | public static List takeRight(List collection, Integer n) { 331 | if (collection == null) { 332 | return new List(); 333 | } 334 | 335 | return slice(collection, collection.size() - n, collection.size()); 336 | } 337 | 338 | /** 339 | * Gets all but the last element of a list 340 | * 341 | * @param collection The list of objects 342 | * @return The new list 343 | */ 344 | public static List initial(List collection) { 345 | if (collection == null || collection.size() - 1 <= 0) { 346 | return new List(); 347 | } 348 | 349 | collection.remove(collection.size() - 1); 350 | 351 | return collection; 352 | } 353 | } -------------------------------------------------------------------------------- /src/classes/lo.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 33.0 4 | -------------------------------------------------------------------------------- /src/classes/lo_test_chunk.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_chunk { 3 | static testMethod void chunk_invalidN_returnsEmptyList() { 4 | List testList = new List{ 5 | 'alpha', 6 | 'bravo', 7 | 'charlie', 8 | 'delta' 9 | }; 10 | Integer n = -1; 11 | 12 | Test.startTest(); 13 | 14 | List> result = lo.chunk(testList, n); 15 | 16 | Test.stopTest(); 17 | 18 | System.assert(result.isEmpty(), 'We should have returned an empty list'); 19 | } 20 | 21 | static testMethod void chunk_evenSplitN_returnsEvenSplit() { 22 | List testList = new List{ 23 | 'alpha', 24 | 'bravo', 25 | 'charlie', 26 | 'delta' 27 | }; 28 | Integer n = 2; 29 | 30 | Test.startTest(); 31 | 32 | List> results = lo.chunk(testList, n); 33 | 34 | Test.stopTest(); 35 | 36 | List> expectedResults = new List>{ 37 | new List{ 38 | 'alpha', 39 | 'bravo' 40 | }, 41 | new List{ 42 | 'charlie', 43 | 'delta' 44 | } 45 | }; 46 | 47 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of chunks back'); 48 | 49 | for (Integer i = 0; i < results.size(); i++) { 50 | System.assertEquals(expectedResults.get(i).size(), results.get(i).size(), 'Did not get the expected size back in chunk ' + i); 51 | 52 | for (Integer j = 0; j < results.get(i).size(); j++) { 53 | System.assertEquals(expectedResults.get(i).get(j), (String)(results.get(i).get(j)), 'Did not get the right data back at chunk ' + i + ' spot ' + j); 54 | } 55 | } 56 | } 57 | 58 | static testMethod void chunk_unevenSplitN_returnsOffsideSplit() { 59 | List testList = new List{ 60 | 'alpha', 61 | 'bravo', 62 | 'charlie', 63 | 'delta' 64 | }; 65 | Integer n = 3; 66 | 67 | Test.startTest(); 68 | 69 | List> results = lo.chunk(testList, n); 70 | 71 | Test.stopTest(); 72 | 73 | List> expectedResults = new List>{ 74 | new List{ 75 | 'alpha', 76 | 'bravo', 77 | 'charlie' 78 | }, 79 | new List{ 80 | 'delta' 81 | } 82 | }; 83 | 84 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of chunks back'); 85 | 86 | for (Integer i = 0; i < results.size(); i++) { 87 | System.assertEquals(expectedResults.get(i).size(), results.get(i).size(), 'Did not get the expected size back in chunk ' + i); 88 | 89 | for (Integer j = 0; j < results.get(i).size(); j++) { 90 | System.assertEquals(expectedResults.get(i).get(j), (String)(results.get(i).get(j)), 'Did not get the right data back at chunk ' + i + ' spot ' + j); 91 | } 92 | } 93 | } 94 | 95 | static testMethod void chunk_largeSplitN_returnsSingleChunk() { 96 | List testList = new List{ 97 | 'alpha', 98 | 'bravo', 99 | 'charlie', 100 | 'delta' 101 | }; 102 | Integer n = 20; 103 | 104 | Test.startTest(); 105 | 106 | List> results = lo.chunk(testList, n); 107 | 108 | Test.stopTest(); 109 | 110 | List> expectedResults = new List>{ 111 | new List{ 112 | 'alpha', 113 | 'bravo', 114 | 'charlie', 115 | 'delta' 116 | } 117 | }; 118 | 119 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of chunks back'); 120 | 121 | for (Integer i = 0; i < results.size(); i++) { 122 | System.assertEquals(expectedResults.get(i).size(), results.get(i).size(), 'Did not get the expected size back in chunk ' + i); 123 | 124 | for (Integer j = 0; j < results.get(i).size(); j++) { 125 | System.assertEquals(expectedResults.get(i).get(j), (String)(results.get(i).get(j)), 'Did not get the right data back at chunk ' + i + ' spot ' + j); 126 | } 127 | } 128 | } 129 | } -------------------------------------------------------------------------------- /src/classes/lo_test_chunk.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 34.0 4 | Active 5 | -------------------------------------------------------------------------------- /src/classes/lo_test_drop.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_drop { 3 | private class DropObject { 4 | public String testString; 5 | 6 | public DropObject(String s) { 7 | this.testString = s; 8 | } 9 | } 10 | 11 | static testMethod void drop_single_noElements_returnsEmptyList() { 12 | List collection = new List(); 13 | 14 | Test.startTest(); 15 | 16 | List results = (List)(lo.drop(collection)); 17 | 18 | Test.stopTest(); 19 | 20 | System.assertEquals(0, results.size(), 'Did not get the expected number of resultss'); 21 | } 22 | 23 | static testMethod void drop_single_oneElement_returnsEmptyList() { 24 | List collection = new List{ 25 | new DropObject('item 1') 26 | }; 27 | 28 | Test.startTest(); 29 | 30 | List results = (List)(lo.drop(collection)); 31 | 32 | Test.stopTest(); 33 | 34 | System.assertEquals(0, results.size(), 'Did not get the expected number of resultss'); 35 | } 36 | 37 | static testMethod void drop_single_threeElements_returnsTwoElements() { 38 | List collection = new List{ 39 | new DropObject('Item 1'), 40 | new DropObject('Item 2'), 41 | new DropObject('Item 3') 42 | }; 43 | 44 | Test.startTest(); 45 | 46 | List results = (List)(lo.drop(collection)); 47 | 48 | Test.stopTest(); 49 | 50 | // Doing this in an ordered list since the result order does matter 51 | List expectedResults = new List{ 52 | 'Item 2', 53 | 'Item 3' 54 | }; 55 | 56 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results'); 57 | 58 | for (Integer i = 0; i < results.size(); i++) { 59 | DropObject result = (DropObject)(results.get(i)); 60 | System.assertEquals(expectedResults.get(i), result.testString, 'Did not get the expected results at ' + i); 61 | } 62 | } 63 | 64 | static testMethod void drop_double_noElements_dropTwo_returnsEmptyList() { 65 | List collection = new List(); 66 | 67 | Test.startTest(); 68 | 69 | List results = (List)(lo.drop(collection, 2)); 70 | 71 | Test.stopTest(); 72 | 73 | System.assertEquals(0, results.size(), 'Did not get the expected number of resultss'); 74 | } 75 | 76 | static testMethod void drop_double_twoElements_dropTwo_returnsEmptyList() { 77 | List collection = new List{ 78 | new DropObject('Item 1'), 79 | new DropObject('Item 2') 80 | }; 81 | 82 | Test.startTest(); 83 | 84 | List results = (List)(lo.drop(collection, 2)); 85 | 86 | Test.stopTest(); 87 | 88 | System.assertEquals(0, results.size(), 'Did not get the expected number of resultss'); 89 | } 90 | 91 | static testMethod void drop_double_tenElements_dropFive_returnsEmptyList() { 92 | List collection = new List{ 93 | new DropObject('Item 1'), 94 | new DropObject('Item 2'), 95 | new DropObject('Item 3'), 96 | new DropObject('Item 4'), 97 | new DropObject('Item 5'), 98 | new DropObject('Item 6'), 99 | new DropObject('Item 7'), 100 | new DropObject('Item 8'), 101 | new DropObject('Item 9'), 102 | new DropObject('Item 10') 103 | }; 104 | 105 | Test.startTest(); 106 | 107 | List results = (List)(lo.drop(collection, 5)); 108 | 109 | Test.stopTest(); 110 | 111 | // Doing this in an ordered list since the result order does matter 112 | List expectedResults = new List{ 113 | 'Item 6', 114 | 'Item 7', 115 | 'Item 8', 116 | 'Item 9', 117 | 'Item 10' 118 | }; 119 | 120 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results'); 121 | 122 | for (Integer i = 0; i < results.size(); i++) { 123 | DropObject result = (DropObject)(results.get(i)); 124 | System.assertEquals(expectedResults.get(i), result.testString, 'Did not get the expected results at ' + i); 125 | } 126 | } 127 | } -------------------------------------------------------------------------------- /src/classes/lo_test_drop.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 33.0 4 | Active 5 | -------------------------------------------------------------------------------- /src/classes/lo_test_dropRight.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_dropRight { 3 | private class DropRightObject { 4 | public String testString; 5 | 6 | public DropRightObject(String s) { 7 | this.testString = s; 8 | } 9 | } 10 | 11 | static testMethod void dropRight_single_noElements_returnsEmptyList() { 12 | List collection = new List(); 13 | 14 | Test.startTest(); 15 | 16 | List results = (List)(lo.dropRight(collection)); 17 | 18 | Test.stopTest(); 19 | 20 | System.assertEquals(0, results.size(), 'Did not get the expected number of resultss'); 21 | } 22 | 23 | static testMethod void dropRight_single_oneElement_returnsEmptyList() { 24 | List collection = new List{ 25 | new DropRightObject('item 1') 26 | }; 27 | 28 | Test.startTest(); 29 | 30 | List results = (List)(lo.dropRight(collection)); 31 | 32 | Test.stopTest(); 33 | 34 | System.assertEquals(0, results.size(), 'Did not get the expected number of resultss'); 35 | } 36 | 37 | static testMethod void dropRight_single_threeElements_returnsTwoElements() { 38 | List collection = new List{ 39 | new DropRightObject('Item 1'), 40 | new DropRightObject('Item 2'), 41 | new DropRightObject('Item 3') 42 | }; 43 | 44 | Test.startTest(); 45 | 46 | List results = (List)(lo.dropRight(collection)); 47 | 48 | Test.stopTest(); 49 | 50 | // Doing this in an ordered list since the result order does matter 51 | List expectedResults = new List{ 52 | 'Item 1', 53 | 'Item 2' 54 | }; 55 | 56 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results'); 57 | 58 | for (Integer i = 0; i < results.size(); i++) { 59 | DropRightObject result = (DropRightObject)(results.get(i)); 60 | System.assertEquals(expectedResults.get(i), result.testString, 'Did not get the expected results at ' + i); 61 | } 62 | } 63 | 64 | static testMethod void dropRight_double_noElements_dropTwo_returnsEmptyList() { 65 | List collection = new List(); 66 | 67 | Test.startTest(); 68 | 69 | List results = (List)(lo.dropRight(collection, 2)); 70 | 71 | Test.stopTest(); 72 | 73 | System.assertEquals(0, results.size(), 'Did not get the expected number of resultss'); 74 | } 75 | 76 | static testMethod void dropRight_double_twoElements_dropTwo_returnsEmptyList() { 77 | List collection = new List{ 78 | new DropRightObject('Item 1'), 79 | new DropRightObject('Item 2') 80 | }; 81 | 82 | Test.startTest(); 83 | 84 | List results = (List)(lo.dropRight(collection, 2)); 85 | 86 | Test.stopTest(); 87 | 88 | System.assertEquals(0, results.size(), 'Did not get the expected number of resultss'); 89 | } 90 | 91 | static testMethod void dropRight_double_tenElements_dropFive_returnsEmptyList() { 92 | List collection = new List{ 93 | new DropRightObject('Item 1'), 94 | new DropRightObject('Item 2'), 95 | new DropRightObject('Item 3'), 96 | new DropRightObject('Item 4'), 97 | new DropRightObject('Item 5'), 98 | new DropRightObject('Item 6'), 99 | new DropRightObject('Item 7'), 100 | new DropRightObject('Item 8'), 101 | new DropRightObject('Item 9'), 102 | new DropRightObject('Item 10') 103 | }; 104 | 105 | Test.startTest(); 106 | 107 | List results = (List)(lo.dropRight(collection, 5)); 108 | 109 | Test.stopTest(); 110 | 111 | // Doing this in an ordered list since the result order does matter 112 | List expectedResults = new List{ 113 | 'Item 1', 114 | 'Item 2', 115 | 'Item 3', 116 | 'Item 4', 117 | 'Item 5' 118 | }; 119 | 120 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results'); 121 | 122 | for (Integer i = 0; i < results.size(); i++) { 123 | DropRightObject result = (DropRightObject)(results.get(i)); 124 | System.assertEquals(expectedResults.get(i), result.testString, 'Did not get the expected results at ' + i); 125 | } 126 | } 127 | } -------------------------------------------------------------------------------- /src/classes/lo_test_dropRight.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 33.0 4 | Active 5 | -------------------------------------------------------------------------------- /src/classes/lo_test_fill.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_fill { 3 | static testMethod void fill_zeroCount_returnsEmptyList() { 4 | String testSource = '_teststring_: 001'; 5 | 6 | Test.startTest(); 7 | 8 | List results = lo.fill(testSource, 0); 9 | 10 | Test.stopTest(); 11 | 12 | System.assertEquals(0, results.size(), 'Did not get the expected number of results'); 13 | } 14 | 15 | static testMethod void fill_tenCount_returnsTenItemsList() { 16 | String testSource = '_teststring_: 001'; 17 | 18 | Test.startTest(); 19 | 20 | List results = lo.fill(testSource, 10); 21 | 22 | Test.stopTest(); 23 | 24 | System.assertEquals(10, results.size(), 'Did not get the expected number of results'); 25 | 26 | for (Integer i = 0; i < results.size(); i++) { 27 | String result = (String)(results.get(i)); 28 | 29 | System.assertEquals(testSource, result, 'Did not get the right object back ' + i); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /src/classes/lo_test_fill.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 33.0 4 | Active 5 | -------------------------------------------------------------------------------- /src/classes/lo_test_initial.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_initial { 3 | 4 | static testMethod void initial_zeroCount_returnsEmptyList() { 5 | List testSource = new List(); 6 | 7 | Test.startTest(); 8 | 9 | List results = lo.initial(testSource); 10 | 11 | Test.stopTest(); 12 | 13 | System.assertEquals(0, results.size(), 'Did not get the expected number of results'); 14 | } 15 | 16 | static testMethod void initial_tenCount_returnsNineItemsList() { 17 | List testSource = new List{ 18 | 'a', 19 | 'b', 20 | 'c', 21 | 'd', 22 | 'e', 23 | 'f', 24 | 'g', 25 | 'h', 26 | 'i', 27 | 'j' 28 | }; 29 | 30 | Test.startTest(); 31 | 32 | List results = lo.initial(testSource); 33 | 34 | Test.stopTest(); 35 | 36 | List expectedResults = new List{ 37 | 'a', 38 | 'b', 39 | 'c', 40 | 'd', 41 | 'e', 42 | 'f', 43 | 'g', 44 | 'h', 45 | 'i' 46 | }; 47 | 48 | System.assertEquals(9, results.size(), 'Did not get the expected number of results'); 49 | System.assertNotEquals('j', results[results.size() - 1], 'Unexpected value for last index'); 50 | 51 | for(Integer i = 0; i < results.size(); i++){ 52 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected value at slot ' + i); 53 | } 54 | } 55 | 56 | static testMethod void initial_nullCollection_returnsEmptyList() { 57 | List testSource; 58 | 59 | Test.startTest(); 60 | 61 | List results = lo.initial(testSource); 62 | 63 | Test.stopTest(); 64 | 65 | System.assertEquals(0, results.size(), 'Did not get the expected number of results'); 66 | } 67 | } -------------------------------------------------------------------------------- /src/classes/lo_test_initial.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 33.0 4 | Active 5 | -------------------------------------------------------------------------------- /src/classes/lo_test_last.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_last { 3 | static testMethod void last_emptyList_returnNull() { 4 | List testList = new List(); 5 | 6 | Test.startTest(); 7 | 8 | Object result = lo.last(testList); 9 | 10 | Test.stopTest(); 11 | 12 | System.assertEquals(null, result, 'Did not get the expected result back'); 13 | } 14 | 15 | static testMethod void last_nullList_returnNull() { 16 | List testList = null; 17 | 18 | Test.startTest(); 19 | 20 | Object result = lo.last(testList); 21 | 22 | Test.stopTest(); 23 | 24 | System.assertEquals(null, result, 'Did not get the expected result back'); 25 | } 26 | 27 | static testMethod void last_populatedList_returnLast() { 28 | List testList = new List{ 29 | 'alpha', 30 | 'bravo', 31 | 'charlie', 32 | 'delta' 33 | }; 34 | 35 | Test.startTest(); 36 | 37 | Object result = lo.last(testList); 38 | 39 | Test.stopTest(); 40 | 41 | System.assertEquals('delta', (String)(result), 'Did not get the expected result back'); 42 | } 43 | } -------------------------------------------------------------------------------- /src/classes/lo_test_last.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 34.0 4 | Active 5 | -------------------------------------------------------------------------------- /src/classes/lo_test_pluck.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_pluck { 3 | private class LevelThree { 4 | public String testString; 5 | 6 | public LevelThree(String s) { 7 | this.testString = s; 8 | } 9 | } 10 | 11 | private class LevelTwo { 12 | public LevelThree three; 13 | 14 | public LevelTwo(String s) { 15 | this.three = new LevelThree('two:' + s); 16 | } 17 | } 18 | 19 | private class LevelOne { 20 | public String testString; 21 | public Integer testInteger; 22 | public LevelTwo two; 23 | 24 | public LevelOne(String s, Integer i) { 25 | this.testString = s; 26 | this.testInteger = i; 27 | this.two = new LevelTwo('one:' + s); 28 | } 29 | } 30 | 31 | static testMethod void pluck_single_usingCustomObjectAndSingleLevelKey_returnsCastableString() { 32 | lo.LOGGING = true; 33 | 34 | LevelOne testObject = new LevelOne('foo', 100); 35 | 36 | Test.startTest(); 37 | 38 | System.assertEquals('foo', (String)(lo.pluck(testObject, 'testString')), 'Did not get the correct result back'); 39 | 40 | Test.stopTest(); 41 | } 42 | 43 | static testMethod void pluck_single_usingCustomObjectAndUnknownKey_throwsException() { 44 | lo.LOGGING = true; 45 | String key = 'unknownKey'; 46 | 47 | LevelOne testObject = new LevelOne('foo', 100); 48 | 49 | Test.startTest(); 50 | 51 | try { 52 | Object result = lo.pluck(testObject, key); 53 | System.assert(false, 'Should have thrown an exception by now'); 54 | } catch (lo.UnknownKeyException e) { 55 | String expectedMessage = lo.MSG_UNKNOWN_KEY + key; 56 | System.assertEquals(expectedMessage, e.getMessage(), 'Got the right exception type but the wrong message'); 57 | } 58 | 59 | Test.stopTest(); 60 | } 61 | 62 | static testMethod void pluck_single_usingCustomObjectAndMultiLevelKey_returnsCastableString() { 63 | lo.LOGGING = true; 64 | 65 | LevelOne testObject = new LevelOne('foo', 100); 66 | 67 | Test.startTest(); 68 | 69 | System.assertEquals('two:one:foo', (String)(lo.pluck(testObject, 'two.three.testString')), 'Did not get the correct result back'); 70 | 71 | Test.stopTest(); 72 | } 73 | 74 | static testMethod void pluck_single_usingCustomObjectAndMultiLevelUnknownKey_throwsException() { 75 | lo.LOGGING = true; 76 | String key = 'unknownKey'; 77 | 78 | LevelOne testObject = new LevelOne('foo', 100); 79 | 80 | Test.startTest(); 81 | 82 | try { 83 | Object result = lo.pluck(testObject, 'two.three.' + key); 84 | System.assert(false, 'Should have thrown an exception by now'); 85 | } catch (lo.UnknownKeyException e) { 86 | String expectedMessage = lo.MSG_UNKNOWN_KEY + key; 87 | System.assertEquals(expectedMessage, e.getMessage(), 'Got the right exception type but the wrong message'); 88 | } 89 | 90 | Test.stopTest(); 91 | } 92 | 93 | static testMethod void pluck_single_usingSObjectAndSingleLevelKey_returnsCastableString() { 94 | String accountName = '_unittest_account_0: ' + Datetime.now().getTime(); 95 | 96 | Account testAccount = new Account(Name = accountName); 97 | 98 | Test.startTest(); 99 | 100 | System.assertEquals(accountName, (String)(lo.pluck(testAccount, 'Name')), 'Did not get the expected result back'); 101 | 102 | Test.stopTest(); 103 | } 104 | 105 | static testMethod void pluck_single_usingSObjectAndMultiLevelKey_returnsCastableString() { 106 | String accountName = '_unittest_account_0: ' + Datetime.now().getTime(); 107 | 108 | Account testAccount = new Account(Name = accountName); 109 | insert testAccount; 110 | 111 | Contact testContact = new Contact( 112 | AccountId = testAccount.Id, 113 | LastName = '_unittest_contact_: ' + Datetime.now().getTime() 114 | ); 115 | insert testContact; 116 | 117 | testContact = [ 118 | select Account.Name 119 | from Contact 120 | where Id = :testContact.Id 121 | ]; 122 | 123 | Test.startTest(); 124 | 125 | System.assertEquals(accountName, (String)(lo.pluck(testContact, 'Account.Name')), 'Did not get the expected result back'); 126 | 127 | Test.stopTest(); 128 | } 129 | 130 | static testMethod void pluck_single_usingSObjectAndUnqueriedField_throwsException() { 131 | String accountName = '_unittest_account_0: ' + Datetime.now().getTime(); 132 | String key = 'AccountNumber'; 133 | 134 | Account testAccount = new Account(Name = accountName); 135 | insert testAccount; 136 | 137 | Contact testContact = new Contact( 138 | AccountId = testAccount.Id, 139 | LastName = '_unittest_contact_: ' + Datetime.now().getTime() 140 | ); 141 | insert testContact; 142 | 143 | testContact = [ 144 | select Account.Name 145 | from Contact 146 | where Id = :testContact.Id 147 | ]; 148 | 149 | Test.startTest(); 150 | 151 | try { 152 | Object result = lo.pluck(testContact, 'Account.' + key); 153 | System.assert(false, 'Should have thrown an exception by now'); 154 | } catch (lo.UnknownKeyException e) { 155 | String expectedMessage = lo.MSG_UNKNOWN_KEY + key; 156 | System.assertEquals(expectedMessage, e.getMessage(), 'Got the right exception type but the wrong message'); 157 | } 158 | 159 | Test.stopTest(); 160 | } 161 | 162 | static testMethod void pluck_list_usingCustomObjectAndSingleLevelKey_returnsListOfCastableStringList() { 163 | lo.LOGGING = true; 164 | 165 | List testList = new List{ 166 | new LevelOne('foo0', 0), 167 | new LevelOne('foo1', 1), 168 | new LevelOne('foo2', 2), 169 | new LevelOne('foo3', 3), 170 | new LevelOne('foo4', 4) 171 | }; 172 | 173 | Test.startTest(); 174 | 175 | List results = lo.pluck(testList, 'testString'); 176 | 177 | Test.stopTest(); 178 | 179 | List expectedResults = new List{ 180 | 'foo0', 181 | 'foo1', 182 | 'foo2', 183 | 'foo3', 184 | 'foo4' 185 | }; 186 | 187 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 188 | 189 | for (Integer i = 0; i < expectedResults.size(); i++) { 190 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the correct result back in slot ' + i); 191 | } 192 | } 193 | 194 | static testMethod void pluck_list_usingCustomObjectAndUnknownKey_returnsEmptyList() { 195 | lo.LOGGING = true; 196 | 197 | List testList = new List{ 198 | new LevelOne('foo0', 0), 199 | new LevelOne('foo1', 1), 200 | new LevelOne('foo2', 2), 201 | new LevelOne('foo3', 3), 202 | new LevelOne('foo4', 4) 203 | }; 204 | 205 | Test.startTest(); 206 | 207 | List results = lo.pluck(testList, 'unknownKey'); 208 | 209 | Test.stopTest(); 210 | 211 | System.assertEquals(0, results.size(), 'Did not get the expected number of results back'); 212 | } 213 | 214 | static testMethod void pluck_list_usingCustomObjectAndMultiLevelKey_returnsListOfCastableStringList() { 215 | lo.LOGGING = true; 216 | 217 | List testList = new List{ 218 | new LevelOne('foo0', 0), 219 | new LevelOne('foo1', 1), 220 | new LevelOne('foo2', 2), 221 | new LevelOne('foo3', 3), 222 | new LevelOne('foo4', 4) 223 | }; 224 | 225 | Test.startTest(); 226 | 227 | List results = lo.pluck(testList, 'two.three.testString'); 228 | 229 | Test.stopTest(); 230 | 231 | List expectedResults = new List{ 232 | 'two:one:foo0', 233 | 'two:one:foo1', 234 | 'two:one:foo2', 235 | 'two:one:foo3', 236 | 'two:one:foo4' 237 | }; 238 | 239 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 240 | 241 | for (Integer i = 0; i < expectedResults.size(); i++) { 242 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the correct result back in slot ' + i); 243 | } 244 | } 245 | 246 | static testMethod void pluck_list_usingCustomObjectAndMultiLevelUnknownKey_returnsEmptyList() { 247 | lo.LOGGING = true; 248 | 249 | List testList = new List{ 250 | new LevelOne('foo0', 0), 251 | new LevelOne('foo1', 1), 252 | new LevelOne('foo2', 2), 253 | new LevelOne('foo3', 3), 254 | new LevelOne('foo4', 4) 255 | }; 256 | 257 | Test.startTest(); 258 | 259 | List results = lo.pluck(testList, 'two.unknownKey.testString'); 260 | 261 | Test.stopTest(); 262 | 263 | System.assertEquals(0, results.size(), 'Did not get the expected number of results back'); 264 | } 265 | 266 | static testMethod void pluck_list_usingSObjectAndSingleLevelKey_returnsListOfCastableStringList() { 267 | List accountNames = new List { 268 | '_unittest_account_0: ' + Datetime.now().getTime(), 269 | '_unittest_account_1: ' + Datetime.now().getTime(), 270 | '_unittest_account_2: ' + Datetime.now().getTime(), 271 | '_unittest_account_3: ' + Datetime.now().getTime(), 272 | '_unittest_account_4: ' + Datetime.now().getTime() 273 | }; 274 | 275 | List testAccounts = new List(); 276 | 277 | for (Integer i = 0; i < accountNames.size(); i++) { 278 | testAccounts.add(new Account(Name = accountNames.get(i))); 279 | } 280 | 281 | Test.startTest(); 282 | 283 | List results = lo.pluck(testAccounts, 'Name'); 284 | 285 | Test.stopTest(); 286 | 287 | System.assertEquals(accountNames.size(), results.size(), 'Did not get the expected number of results'); 288 | 289 | for (Integer i = 0; i < accountNames.size(); i++) { 290 | System.assertEquals(accountNames.get(i), (String)(results.get(i)), 'Did not get the epxceted result at slot ' + i); 291 | } 292 | } 293 | 294 | static testMethod void pluck_list_usingSObjectAndMultiLevelKey_returnsListOfCastableStringList() { 295 | Integer objCount = 5; 296 | 297 | List accountNames = new List { 298 | '_unittest_account_0: ' + Datetime.now().getTime(), 299 | '_unittest_account_1: ' + Datetime.now().getTime(), 300 | '_unittest_account_2: ' + Datetime.now().getTime(), 301 | '_unittest_account_3: ' + Datetime.now().getTime(), 302 | '_unittest_account_4: ' + Datetime.now().getTime() 303 | }; 304 | 305 | List testAccounts = new List(); 306 | 307 | for (Integer i = 0; i < objCount; i++) { 308 | testAccounts.add(new Account(Name = accountNames.get(i))); 309 | } 310 | 311 | insert testAccounts; 312 | 313 | List testContacts = new List(); 314 | 315 | for (Integer i = 0; i < objCount; i++) { 316 | testContacts.add(new Contact( 317 | AccountId = testAccounts.get(i).Id, 318 | LastName = '_unittest_contact_' + i + ': ' + Datetime.now().getTime() 319 | )); 320 | } 321 | 322 | insert testContacts; 323 | 324 | List testCases = new List(); 325 | 326 | for (Integer i = 0; i < objCount; i++) { 327 | testCases.add(new Case( 328 | ContactId = testContacts.get(i).Id, 329 | Subject = '_unittest_case_' + i + ': ' + Datetime.now().getTime() 330 | )); 331 | } 332 | 333 | insert testCases; 334 | 335 | Map caseMap = new Map(testCases); 336 | 337 | List testCollection = [ 338 | select Contact.Account.Name 339 | from Case 340 | where Id in :caseMap.keySet() 341 | order by Contact.Account.Name 342 | ]; 343 | 344 | Test.startTest(); 345 | 346 | List results = lo.pluck(testCollection, 'Contact.Account.Name'); 347 | 348 | Test.stopTest(); 349 | 350 | System.assertEquals(accountNames.size(), results.size(), 'Did not get the expected number of results'); 351 | 352 | for (Integer i = 0; i < accountNames.size(); i++) { 353 | System.assertEquals(accountNames.get(i), (String)(results.get(i)), 'Did not get the epxceted result at slot ' + i); 354 | } 355 | } 356 | 357 | static testMethod void pluck_list_usingSObjectAndMultiLevelUnqueriedField_returnsEmptyList() { 358 | Integer objCount = 5; 359 | 360 | List accountNames = new List { 361 | '_unittest_account_0: ' + Datetime.now().getTime(), 362 | '_unittest_account_1: ' + Datetime.now().getTime(), 363 | '_unittest_account_2: ' + Datetime.now().getTime(), 364 | '_unittest_account_3: ' + Datetime.now().getTime(), 365 | '_unittest_account_4: ' + Datetime.now().getTime() 366 | }; 367 | 368 | List testAccounts = new List(); 369 | 370 | for (Integer i = 0; i < objCount; i++) { 371 | testAccounts.add(new Account(Name = accountNames.get(i))); 372 | } 373 | 374 | insert testAccounts; 375 | 376 | List testContacts = new List(); 377 | 378 | for (Integer i = 0; i < objCount; i++) { 379 | testContacts.add(new Contact( 380 | AccountId = testAccounts.get(i).Id, 381 | LastName = '_unittest_contact_' + i + ': ' + Datetime.now().getTime() 382 | )); 383 | } 384 | 385 | insert testContacts; 386 | 387 | List testCases = new List(); 388 | 389 | for (Integer i = 0; i < objCount; i++) { 390 | testCases.add(new Case( 391 | ContactId = testContacts.get(i).Id, 392 | Subject = '_unittest_case_' + i + ': ' + Datetime.now().getTime() 393 | )); 394 | } 395 | 396 | insert testCases; 397 | 398 | Map caseMap = new Map(testCases); 399 | 400 | List testCollection = [ 401 | select Contact.Account.Name 402 | from Case 403 | where Id in :caseMap.keySet() 404 | order by Contact.Account.Name 405 | ]; 406 | 407 | Test.startTest(); 408 | 409 | List results = lo.pluck(testCollection, 'Contact.Account.AccountNumber'); 410 | 411 | Test.stopTest(); 412 | 413 | System.assertEquals(0, results.size(), 'Did not get the expected number of results'); 414 | } 415 | } -------------------------------------------------------------------------------- /src/classes/lo_test_pluck.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 33.0 4 | -------------------------------------------------------------------------------- /src/classes/lo_test_rest.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_rest { 3 | static testMethod void rest_nullList_returnsEmptyList() { 4 | List testList = null; 5 | 6 | Test.startTest(); 7 | 8 | List results = lo.rest(testList); 9 | 10 | Test.stopTest(); 11 | 12 | System.assert(results.isEmpty(), 'Did not get the expected results back'); 13 | } 14 | 15 | static testMethod void rest_emptyList_returnsEmptyList() { 16 | List testList = new List(); 17 | 18 | Test.startTest(); 19 | 20 | List results = lo.rest(testList); 21 | 22 | Test.stopTest(); 23 | 24 | System.assert(results.isEmpty(), 'Did not get the expected results back'); 25 | } 26 | 27 | static testMethod void rest_singleItemList_returnsEmptyList() { 28 | List testList = new List{ 29 | 'alpha' 30 | }; 31 | 32 | Test.startTest(); 33 | 34 | List results = lo.rest(testList); 35 | 36 | Test.stopTest(); 37 | 38 | System.assert(results.isEmpty(), 'Did not get the expected results back'); 39 | } 40 | 41 | static testMethod void rest_multiItemList_returnsCorrectList() { 42 | List testList = new List{ 43 | 'alpha', 44 | 'bravo', 45 | 'charlie', 46 | 'delta' 47 | }; 48 | 49 | Test.startTest(); 50 | 51 | List results = lo.rest(testList); 52 | 53 | Test.stopTest(); 54 | 55 | List expectedResults = new List{ 56 | 'bravo', 57 | 'charlie', 58 | 'delta' 59 | }; 60 | 61 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected results back'); 62 | 63 | for (Integer i = 0; i < results.size(); i++) { 64 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected result back in slot ' + i); 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /src/classes/lo_test_rest.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 34.0 4 | Active 5 | -------------------------------------------------------------------------------- /src/classes/lo_test_slice.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_slice { 3 | static testMethod void slice_startAndStop_nullList_returnsEmptyList() { 4 | List testList = null; 5 | Integer start = 1; 6 | Integer stop = 3; 7 | 8 | Test.startTest(); 9 | 10 | List results = lo.slice(testList, start, stop); 11 | 12 | Test.stopTest(); 13 | 14 | System.assert(results.isEmpty(), 'Did not get the expected number of results back'); 15 | } 16 | 17 | static testMethod void slice_startAndStop_emptyList_returnsEmptyList() { 18 | List testList = new List(); 19 | Integer start = 1; 20 | Integer stop = 3; 21 | 22 | Test.startTest(); 23 | 24 | List results = lo.slice(testList, start, stop); 25 | 26 | Test.stopTest(); 27 | 28 | System.assert(results.isEmpty(), 'Did not get the expected number of results back'); 29 | } 30 | 31 | static testMethod void slice_startAndStop_startGreaterThanStop_returnsEmptyList() { 32 | List testList = new List{ 33 | 'alpha', 34 | 'bravo', 35 | 'charlie', 36 | 'delta' 37 | }; 38 | Integer start = 3; 39 | Integer stop = 1; 40 | 41 | Test.startTest(); 42 | 43 | List results = lo.slice(testList, start, stop); 44 | 45 | Test.stopTest(); 46 | 47 | System.assert(results.isEmpty(), 'Did not get the expected number of results back'); 48 | } 49 | 50 | static testMethod void slice_startAndStop_startLessThanZero_returnsCorrectList() { 51 | List testList = new List{ 52 | 'alpha', 53 | 'bravo', 54 | 'charlie', 55 | 'delta' 56 | }; 57 | Integer start = -1; 58 | Integer stop = 3; 59 | 60 | Test.startTest(); 61 | 62 | List results = lo.slice(testList, start, stop); 63 | 64 | Test.stopTest(); 65 | 66 | List expectedResults = new List{ 67 | 'alpha', 68 | 'bravo', 69 | 'charlie' 70 | }; 71 | 72 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 73 | 74 | for (Integer i = 0; i < results.size(); i++) { 75 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected value at slot ' + i); 76 | } 77 | } 78 | 79 | static testMethod void slice_startAndStop_stopGreaterThanListSize_returnsCorrectList() { 80 | List testList = new List{ 81 | 'alpha', 82 | 'bravo', 83 | 'charlie', 84 | 'delta' 85 | }; 86 | Integer start = 1; 87 | Integer stop = 20; 88 | 89 | Test.startTest(); 90 | 91 | List results = lo.slice(testList, start, stop); 92 | 93 | Test.stopTest(); 94 | 95 | List expectedResults = new List{ 96 | 'bravo', 97 | 'charlie', 98 | 'delta' 99 | }; 100 | 101 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 102 | 103 | for (Integer i = 0; i < results.size(); i++) { 104 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected value at slot ' + i); 105 | } 106 | } 107 | 108 | static testMethod void slice_startAndStop_correctInputs_returnsCorrectList() { 109 | List testList = new List{ 110 | 'alpha', 111 | 'bravo', 112 | 'charlie', 113 | 'delta' 114 | }; 115 | Integer start = 1; 116 | Integer stop = 3; 117 | 118 | Test.startTest(); 119 | 120 | List results = lo.slice(testList, start, stop); 121 | 122 | Test.stopTest(); 123 | 124 | List expectedResults = new List{ 125 | 'bravo', 126 | 'charlie' 127 | }; 128 | 129 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 130 | 131 | for (Integer i = 0; i < results.size(); i++) { 132 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected value at slot ' + i); 133 | } 134 | } 135 | 136 | static testMethod void slice_start_nullList_returnsEmptyList() { 137 | List testList = null; 138 | Integer start = 1; 139 | 140 | Test.startTest(); 141 | 142 | List results = lo.slice(testList, start); 143 | 144 | Test.stopTest(); 145 | 146 | System.assert(results.isEmpty(), 'Did not get the expected number of results back'); 147 | } 148 | 149 | static testMethod void slice_start_emptyList_returnsEmptyList() { 150 | List testList = new List(); 151 | Integer start = 1; 152 | 153 | Test.startTest(); 154 | 155 | List results = lo.slice(testList, start); 156 | 157 | Test.stopTest(); 158 | 159 | System.assert(results.isEmpty(), 'Did not get the expected number of results back'); 160 | } 161 | 162 | static testMethod void slice_start_startGreaterThanListSize_returnsEmptyList() { 163 | List testList = new List{ 164 | 'alpha', 165 | 'bravo', 166 | 'charlie', 167 | 'delta' 168 | }; 169 | Integer start = 20; 170 | 171 | Test.startTest(); 172 | 173 | List results = lo.slice(testList, start); 174 | 175 | Test.stopTest(); 176 | 177 | System.assert(results.isEmpty(), 'Did not get the expected number of results back'); 178 | } 179 | 180 | static testMethod void slice_start_startLessThanZero_returnsCorrectList() { 181 | List testList = new List{ 182 | 'alpha', 183 | 'bravo', 184 | 'charlie', 185 | 'delta' 186 | }; 187 | Integer start = -1; 188 | 189 | Test.startTest(); 190 | 191 | List results = lo.slice(testList, start); 192 | 193 | Test.stopTest(); 194 | 195 | List expectedResults = new List{ 196 | 'alpha', 197 | 'bravo', 198 | 'charlie', 199 | 'delta' 200 | }; 201 | 202 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 203 | 204 | for (Integer i = 0; i < results.size(); i++) { 205 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected value at slot ' + i); 206 | } 207 | } 208 | 209 | static testMethod void slice_start_correctInputs_returnsCorrectList() { 210 | List testList = new List{ 211 | 'alpha', 212 | 'bravo', 213 | 'charlie', 214 | 'delta' 215 | }; 216 | Integer start = 1; 217 | 218 | Test.startTest(); 219 | 220 | List results = lo.slice(testList, start); 221 | 222 | Test.stopTest(); 223 | 224 | List expectedResults = new List{ 225 | 'bravo', 226 | 'charlie', 227 | 'delta' 228 | }; 229 | 230 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 231 | 232 | for (Integer i = 0; i < results.size(); i++) { 233 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected value at slot ' + i); 234 | } 235 | } 236 | } -------------------------------------------------------------------------------- /src/classes/lo_test_slice.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 34.0 4 | Active 5 | -------------------------------------------------------------------------------- /src/classes/lo_test_take.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_take { 3 | static testMethod void take_emptyList_returnsEmptyList() { 4 | List collection = new List(); 5 | 6 | Test.startTest(); 7 | 8 | List results = lo.take(collection, 3); 9 | 10 | Test.stopTest(); 11 | 12 | System.assert(results.isEmpty(), 'Should have gotten an empty list back'); 13 | } 14 | 15 | static testMethod void take_nullList_returnsEmptyList() { 16 | List collection = null; 17 | 18 | Test.startTest(); 19 | 20 | List results = lo.take(collection, 3); 21 | 22 | Test.stopTest(); 23 | 24 | System.assert(results.isEmpty(), 'Should have gotten an empty list back'); 25 | } 26 | 27 | static testMethod void take_populatedListTooFew_returnsEmptyList() { 28 | List collection = new List{ 29 | 'alpha', 30 | 'bravo', 31 | 'charlie', 32 | 'delta', 33 | 'echo' 34 | }; 35 | 36 | Test.startTest(); 37 | 38 | List results = lo.take(collection, -10); 39 | 40 | Test.stopTest(); 41 | 42 | System.assert(results.isEmpty(), 'Should not have gotten any results back'); 43 | } 44 | 45 | static testMethod void take_populatedList_returnsPartialList() { 46 | List collection = new List{ 47 | 'alpha', 48 | 'bravo', 49 | 'charlie', 50 | 'delta', 51 | 'echo' 52 | }; 53 | 54 | Test.startTest(); 55 | 56 | List results = lo.take(collection, 3); 57 | 58 | Test.stopTest(); 59 | 60 | List expectedResults = new List{ 61 | 'alpha', 62 | 'bravo', 63 | 'charlie' 64 | }; 65 | 66 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 67 | 68 | for (Integer i = 0; i < results.size(); i++) { 69 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected result at slot ' + i); 70 | } 71 | } 72 | 73 | static testMethod void take_populatedListTooMany_returnsFullList() { 74 | List collection = new List{ 75 | 'alpha', 76 | 'bravo', 77 | 'charlie', 78 | 'delta', 79 | 'echo' 80 | }; 81 | 82 | Test.startTest(); 83 | 84 | List results = lo.take(collection, 100); 85 | 86 | Test.stopTest(); 87 | 88 | List expectedResults = new List{ 89 | 'alpha', 90 | 'bravo', 91 | 'charlie', 92 | 'delta', 93 | 'echo' 94 | }; 95 | 96 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 97 | 98 | for (Integer i = 0; i < results.size(); i++) { 99 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected result at slot ' + i); 100 | } 101 | } 102 | } -------------------------------------------------------------------------------- /src/classes/lo_test_take.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 34.0 4 | Active 5 | -------------------------------------------------------------------------------- /src/classes/lo_test_takeRight.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_takeRight { 3 | static testMethod void takeRight_emptyList_returnsEmptyList() { 4 | List collection = new List(); 5 | 6 | Test.startTest(); 7 | 8 | List results = lo.takeRight(collection, 3); 9 | 10 | Test.stopTest(); 11 | 12 | System.assert(results.isEmpty(), 'Should have gotten an empty list back'); 13 | } 14 | 15 | static testMethod void takeRight_nullList_returnsEmptyList() { 16 | List collection = null; 17 | 18 | Test.startTest(); 19 | 20 | List results = lo.takeRight(collection, 3); 21 | 22 | Test.stopTest(); 23 | 24 | System.assert(results.isEmpty(), 'Should have gotten an empty list back'); 25 | } 26 | 27 | static testMethod void takeRight_populatedListTooFew_returnsEmptyList() { 28 | List collection = new List{ 29 | 'alpha', 30 | 'bravo', 31 | 'charlie', 32 | 'delta', 33 | 'echo' 34 | }; 35 | 36 | Test.startTest(); 37 | 38 | List results = lo.takeRight(collection, -10); 39 | 40 | Test.stopTest(); 41 | 42 | System.assert(results.isEmpty(), 'Should not have gotten any results back'); 43 | } 44 | 45 | static testMethod void takeRight_populatedList_returnsPartialList() { 46 | List collection = new List{ 47 | 'alpha', 48 | 'bravo', 49 | 'charlie', 50 | 'delta', 51 | 'echo' 52 | }; 53 | 54 | Test.startTest(); 55 | 56 | List results = lo.takeRight(collection, 3); 57 | 58 | Test.stopTest(); 59 | 60 | List expectedResults = new List{ 61 | 'charlie', 62 | 'delta', 63 | 'echo' 64 | }; 65 | 66 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 67 | 68 | for (Integer i = 0; i < results.size(); i++) { 69 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected result at slot ' + i); 70 | } 71 | } 72 | 73 | static testMethod void takeRight_populatedListTooMany_returnsFullList() { 74 | List collection = new List{ 75 | 'alpha', 76 | 'bravo', 77 | 'charlie', 78 | 'delta', 79 | 'echo' 80 | }; 81 | 82 | Test.startTest(); 83 | 84 | List results = lo.takeRight(collection, 100); 85 | 86 | Test.stopTest(); 87 | 88 | List expectedResults = new List{ 89 | 'alpha', 90 | 'bravo', 91 | 'charlie', 92 | 'delta', 93 | 'echo' 94 | }; 95 | 96 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 97 | 98 | for (Integer i = 0; i < results.size(); i++) { 99 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected result at slot ' + i); 100 | } 101 | } 102 | } -------------------------------------------------------------------------------- /src/classes/lo_test_takeRight.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 34.0 4 | Active 5 | -------------------------------------------------------------------------------- /src/classes/lo_test_tostring.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_tostring { 3 | static testMethod void toString_nullCollection_returnsNull() { 4 | List collection = null; 5 | 6 | Test.startTest(); 7 | 8 | List results = lo.toString(collection); 9 | 10 | Test.stopTest(); 11 | 12 | System.assertEquals(null, results, 'The returned collection should be null'); 13 | } 14 | 15 | static testMethod void toString_emptyCollection_returnsEmptyList() { 16 | List collection = new List(); 17 | 18 | Test.startTest(); 19 | 20 | List results = lo.toString(collection); 21 | 22 | Test.stopTest(); 23 | 24 | System.assert(results.isEmpty(), 'The returned collection should be empty'); 25 | } 26 | 27 | static testMethod void toString_validCollection_returnsCastedList() { 28 | List expectedResults = new List{ 29 | 'Test string 1', 30 | 'Test string 2', 31 | 'Test string 3', 32 | 'Test string 4', 33 | 'Test string 5' 34 | }; 35 | 36 | List collection = (List)(JSON.deserializeUntyped(JSON.serialize(expectedResults))); 37 | 38 | Test.startTest(); 39 | 40 | List results = lo.toString(collection); 41 | 42 | Test.stopTest(); 43 | 44 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results'); 45 | 46 | for (Integer i = 0; i < expectedResults.size(); i++) { 47 | System.assertEquals(expectedResults.get(i), results.get(i), 'Did not get the correct result back in slot ' + i); 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /src/classes/lo_test_tostring.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 33.0 4 | Active 5 | -------------------------------------------------------------------------------- /src/classes/lo_test_union.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class lo_test_union { 3 | static testMethod void union_nullCollection_returnsEmptyCollection() { 4 | List> collection = null; 5 | 6 | Test.startTest(); 7 | 8 | List results = lo.union(collection); 9 | 10 | Test.stopTest(); 11 | 12 | System.assert(results.isEmpty(), 'Should have gotten an empty collection back'); 13 | } 14 | 15 | static testMethod void union_emptyCollection_returnsEmptyCollection() { 16 | List> collection = new List>(); 17 | 18 | Test.startTest(); 19 | 20 | List results = lo.union(collection); 21 | 22 | Test.stopTest(); 23 | 24 | System.assert(results.isEmpty(), 'Should have gotten an empty collection back'); 25 | } 26 | 27 | static testMethod void union_singleCollection_returnsSingleCollection() { 28 | List> collection = new List>{ 29 | new List { 30 | 'alpha', 31 | 'bravo' 32 | } 33 | }; 34 | 35 | Test.startTest(); 36 | 37 | List results = lo.union(collection); 38 | 39 | Test.stopTest(); 40 | 41 | List expectedResults = new List{ 42 | 'alpha', 43 | 'bravo' 44 | }; 45 | 46 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 47 | 48 | for (Integer i = 0; i < results.size(); i++) { 49 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected result back at index ' + i); 50 | } 51 | } 52 | 53 | static testMethod void union_multipleCollection_returnsSingleCollection() { 54 | List> collection = new List>{ 55 | new List { 56 | 'alpha', 57 | 'bravo' 58 | }, 59 | new List { 60 | 'charlie', 61 | 'delta' 62 | }, 63 | new List(), 64 | new List { 65 | 'echo' 66 | } 67 | }; 68 | 69 | Test.startTest(); 70 | 71 | List results = lo.union(collection); 72 | 73 | Test.stopTest(); 74 | 75 | List expectedResults = new List{ 76 | 'alpha', 77 | 'bravo', 78 | 'charlie', 79 | 'delta', 80 | 'echo' 81 | }; 82 | 83 | System.assertEquals(expectedResults.size(), results.size(), 'Did not get the expected number of results back'); 84 | 85 | for (Integer i = 0; i < results.size(); i++) { 86 | System.assertEquals(expectedResults.get(i), (String)(results.get(i)), 'Did not get the expected result back at index ' + i); 87 | } 88 | } 89 | } -------------------------------------------------------------------------------- /src/classes/lo_test_union.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 34.0 4 | Active 5 | -------------------------------------------------------------------------------- /src/package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | * 5 | ApexClass 6 | 7 | 8 | * 9 | ApexComponent 10 | 11 | 12 | * 13 | ApexPage 14 | 15 | 16 | * 17 | ApexTrigger 18 | 19 | 20 | * 21 | StaticResource 22 | 23 | 29.0 24 | --------------------------------------------------------------------------------