Skip to content

Instantly share code, notes, and snippets.

@AlwaysThinkin
Last active May 17, 2019 23:14
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AlwaysThinkin/abd90faec67743830e534f8aa413d71c to your computer and use it in GitHub Desktop.
Save AlwaysThinkin/abd90faec67743830e534f8aa413d71c to your computer and use it in GitHub Desktop.
@isTest
public class Collections {
static testMethod void Lists(){
//LISTS ARE AN ORDERED COLLECTION OF DATA
//How to instantiate a List with some pre-defined values
String someword = 'thread';
List<String> threeStrings = new List<String>{'rope', 'twine', someword};
//Lists have methods that allow us to evaluate or manipulate them
//See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_list.htm
System.assertEquals(3, threeStrings.size());
//List items can be referenced by their "index" number starting with zero
System.assertEquals('rope', threeStrings[0]);
//How to Instantiate an empty List
List<String> noStrings = new List<String>();
System.assert(noStrings.isEmpty());
noStrings.add('rock');
System.assertEquals(1, noStrings.size());
//FIX ME! Using the values in the first List we made, uncomment the two lines below and get them to pass
System.assertEquals('rope', threeStrings[0]);
//System.assertEquals(, threeStrings[1]);
//System.assertEquals('thread', threeStrings[]);
//Lists can also contain other lists. Think of an Accounts & Contacts Report.
List<List<String>> numberNames = new List<List<String>>();
numberNames.add(new List<String>{'zero', 'one', 'two', 'three', 'four'});
numberNames.add(new List<String>{'cero', 'uno', 'dos', 'tres'});
numberNames.add(new List<String>{'ling', 'yi', 'er'});
System.assertEquals(3, numberNames.size());
//FIX ME! Using the data in numberNames uncomment the lines below and get them to pass
//System.assertEquals(, numberNames[2].size());
//System.assertEquals(4, numberNames[].size());
//Index notation can be chained to retrieve values in lists of lists
System.assertEquals('zero', numberNames[0][0]);
//FIX ME! Build the String in the expected values - don't forget to concatenate spaces into your string!
//System.assertEquals('uno dos tres', );
//Creating a List from a SOQL query is very common
List<Profile> profs = [SELECT Name FROM Profile];
System.debug('All the Profiles: ' + profs);
//Note that even though we did not add ID to the SELECT, it's retreived:
System.debug('Profile 1 ID: ' + profs[0].Id);
//But any other field must be added to the SELECT to be referenced:
//System.debug('THIS WILL NOT WORK!' + profs[0].CreatedDate);
//FIX ME! Make the following assertion pass by figuring out a way to count all the Profiles
//System.assertEquals( , profs.size());
//Alternate method for creating a List: datatype first, followed by []
//This allows you to set a "dimension" that forces the List size to be validated.
String[] oneWord = new String[1];
oneWord[0] = 'able';
//oneWord[1] = 'baker';//Won't Work! The List was configured for only 1 item
oneWord.add('charlie');//Will Work! The .add() method automatically expands the List
system.debug(oneWord);
oneWord[1] = 'baker';
System.debug(oneWord);
System.assertEquals(2, oneWord.size());
}
static testMethod void Sets(){
//SETS ARE AN UNORDERED COLLECTION OF UNIQUE DATA
Set<String> planets = new Set<String>{'Mars', 'Earth', 'Pluto'};
System.assertEquals(3, planets.size());
//Sets enforce uniqueness so you don't have to check whether a value is already in a Set
Set<String> months = new Set<String>();
months.add('June');
months.add('July');
months.add('June');
System.assertEquals(2, months.size());
//Sets have some similar methods to Lists like add() and some useful others like contains()
//See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm
System.assert(months.contains('July'));
//Sets are usually created from other collections like Lists
//Here are two methods for adding a List to a Set
//1. Add an existing list when instantiating the Set
List<ID> list0 = new List<ID>{'00Q3600000CqgXh', '00Q3600000yTEaB','00Q3600000RiGht'};
Set<ID> setA = new Set<ID>(list0);
//2. Use a Set Method to add a List (or another Set)
List<ID> list1 = new List<ID>{'00Q3600000zUCaR', '00Q3600000QpjNq','00Q3600000RiGht'};
setA.addAll(list1);
//FIX ME! Write System Assert methods to confirm that setA contains all the elements from list0 and list1
}
static testMethod void Maps(){
//MAPS ARE NAMED COLLECTIONS OF DATA
Map<String, String> fieldValues = new Map<String, String>{'Name' => '2u, Inc.', 'Website' => '2u.com', 'Phone' => '301-892-4350'};
System.assertEquals(3, fieldValues.size());
//Maps are useful to relate something you know, to something you want to know.
//Here we map an integer (we know) to a string which holds the Mandarin word (we don't know) for the number.
Map<Integer, String> numbers = new Map<Integer, String>();
numbers.put(1, 'Yi');
numbers.put(2, 'Er');
numbers.put(3, 'San');
//Notice how we use "put" instead of "add"? Same purpose, just different name.
//See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm
//The power of maps comes from the get() method which allows us to use the "key", something we know,
//to get the "value", something we don't know.
System.debug(numbers.get(1));
//FIX ME! Using the data in the numbers map, uncomment the lines below and get them to pass
//System.assertEquals('Er', numbers.get());
//System.assertEquals(, numbers.get(3));
System.debug(numbers.keySet());
//FIX ME! Populate the Set called 'mynumbers' that you can then use to get the assert to pass
Set<Integer> mynumbers;
//System.assertEquals(numbers.keySet(),mynumbers);
//FIX ME! Populate the List called 'values' that you can then use to get the assert to pass
List<String> values;
//System.assertEquals(numbers.values(), values);
//FIX ME! On the Map Methods page in the Apex docs, find the method that allows you to remove an item from the Map
// and get the assertion to pass.
//numbers.???
//System.assertEquals(2, numbers.size());
//FIX ME! And now find and use the method to remove all the values from the Map
// and get the assertion to pass.
//numbers.???
//System.assert(numbers.isEmpty());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment