---
Help: Fixture to define and manipulate maps (i.e. hashes) in the wiki
---
Map Fixture allows Maps to be defined and manipulated in the wiki. It is usable both in [[script tables][#scripts]] and [[dynamic decision tables][#ddt]].

!anchor ddt

!2 Dynamic Decision Table Usage
The first examples show usage of the fixture in [[dynamic decision tables][.FitNesse.UserGuide.WritingAcceptanceTests.SliM.DynamicDecisionTable]].
This allows a map to be defined based on a row of a wiki table, [[Map of Maps Fixture][MapOfMapsFixture]] allows maps to be defined based on the columns of a table.

!3 Define a Map
Here we define a map (and store it in a Slim variable) using simple values and a list (indicated by the header name ending on '[]'), using a dynamic decision table. Any output column (header name ending on '?') will receive a copy of the map defined.

|ddt: map fixture                                                 |
|a|b|email           |aliases[]                           |hash?  |
|1|2|fitnesse@hsac.nl|github@hsac.nl, !-FitNesse@hsac.nl-!|$hash1=|

!3 Nested maps
Here we define a map with a nested map under the 'map' key.

|ddt: map fixture                                                                         |
|c             |map   |url                                                        |map?   |
|{{{ <xml/> }}}|$hash1|https://github.com/fhoeben/hsac-fitnesse-fixtures/releases/|$hash2=|
|3             |$hash2|https://github.com/fhoeben/hsac-fitnesse-fixtures/wiki     |$hash3=|

!anchor scripts

!2 Maps in a Script
Here we manipulate a map using [[script tables][.FitNesse.UserGuide.WritingAcceptanceTests.SliM.ScriptTable]]. This can be done either with or without a map to manipulate in the script's first row.

!3 Map Supplied in Constructor
The map to use is supplied using a constructor argument to the fixture. First we use 'hash3' defined above and then 'hash4', created by the first script.

Values for lists can either be set using the 'set values' method or by specifying a key name ending on '[]'. (The '[]' will not be included in the key name. If you do want a key name ending on '[]' you have to specify '\[]', its content will not be seen as a list)

|script     |map fixture        |$hash3                                   |
|check      |value              |c         |3                             |
|check      |size               |3                                        |
|clear value|c                                                            |
|check      |size               |2                                        |
|check      |value              |c         |null                          |
|reject     |clear value        |c                                        |
|set value  |6                  |for       |d                             |
|check      |size               |3                                        |
|set values |7,8                |for       |e                             |
|set value  |9,asas             |for       |f[]                           |
|set value  |9,asas             |for       |g\[]                          |
|check      |value              |map.map.b |2                             |
|set value  |3                  |for       |map.map.b                     |
|ensure     |clear value        |map.map.b                                |
|check      |value              |map.map.b |null                          |
|reject     |clear value        |map.map.b                                |
|set value  |3                  |for       |map.map.b                     |
|check      |value              |b         |in|$hash1|3                   |
|check      |value              |aliases[1]|in|$hash1|!-FitNesse@hsac.nl-!|
|set value  |!-github3@hsac.nl-!|for       |map.map.aliases[1]            |
|check      |value              |aliases[1]|in|$hash1|!-github3@hsac.nl-! |
|check      |value              |e[0]      |7                             |
|check      |value              |f[1]      |asas                          |
|check      |value              |g[]       |9,asas                        |
|$hash4=    |value              |map.map                                  |
|check      |size of            |map.map   |4                             |
|$allKeys2= |all keys                                                     |
|show       |all values                                                   |
|$hash5=    |copy map                                                     |

|script        |map fixture   |$hash4                       |
|check         |value         |b         |3                 |
|$aliases=     |value         |aliases                      |
|$aliases2=    |value         |aliases[]                    |
|check         |value         |aliases[0]|!-github@hsac.nl-!|
|check         |value         |aliases[2]|null              |
|check         |size of       |aliases   |2                 |
|check         |size of       |aliases[] |2                 |
|content equals|$hash1                                      |
|reject        |content equals|$hash2                       |

!3 Map as Argument
Here we supply no map to the constructor, but pass the one to use in each script row or use the newly (empty) implicitly created map.

|script    |map fixture                                                                                |
|check     |value                |b                |in           |$hash4      |3                       |
|set value |4                    |for              |b            |in          |$hash4                  |
|note      |'''the wiki does not show the updated value for $hash4.b, but it is set correctly!'''      |
|check     |value                |b                |in           |$hash4      |4                       |
|check     |value                |map.map.a        |in           |$hash3      |1                       |
|check     |size of              |aliases          |in           |$hash1      |2                       |
|set values|mytest@hsac.nl       |for              |aliases      |in          |$hash1                  |
|note      |'''the wiki does not show the updated value for $hash1.aliases, but it is set correctly!'''|
|check     |size of              |aliases          |in           |$hash1      |1                       |
|check     |value                |aliases[0]       |in           |$hash1      |!-mytest@hsac.nl-!      |
|ensure    |content of           |$hash1           |equals       |$hash4                               |
|reject    |content of           |$hash2           |equals       |$hash4                               |
|check     |size of              |$hash1           |4                                                  |
|set value |a                    |for              |b                                                  |
|set value |c                    |for              |a                                                  |
|$hash6=   |copy map                                                                                   |

!2 Not Expanding . in Name to Nested Map

Normally when setting values '.' in a name is interpreted to mean a nested map. But this can be disabled.

|script   |map fixture                                                                                |
|ensure   |expands periods in names to nested maps                                                    |
|set value|2            |for                                   |browser.download.folderList           |
|set value|sheetml.sheet|for                                   |browser.helperApps.neverAsk.saveToDisk|
|$browser=|value        |browser                               |                                      |
|check    |size of      |$browser                              |2                                     |
|check    |value        |browser.download.folderList           |2                                     |
|check    |value        |browser.helperApps.neverAsk.saveToDisk|sheetml.sheet                         |



|script                                |map fixture                                                                                |
|expand periods in names to nested maps|false                                                                                      |
|reject                                |expands periods in names to nested maps                                                    |
|set value                             |2            |for                                   |browser.download.folderList           |
|set value                             |sheetml.sheet|for                                   |browser.helperApps.neverAsk.saveToDisk|
|check                                 |value        |browser                               |null                                  |
|check                                 |value        |browser.download.folderList           |2                                     |
|check                                 |value        |browser.helperApps.neverAsk.saveToDisk|sheetml.sheet                         |

!2 Adding elements to a list

!note We ensure we display lists numbered, because otherwise lists of maps look quite bad in wiki.
|script  |list fixture |
|display lists numbered|

|script    |map fixture                       |
|          |Add to existing list              |
|set values|7,8               |for       |a   |
|add value |9                 |to        |a   |
|check     |value             |a[2]      |9   |
|          |Add to new list                   |
|add value |1                 |to        |b   |
|add value |2                 |to        |b   |
|check     |value             |b[1]      |2   |
|          |Add a map to an existing list     |
|set value |$hash1            |for       |c[0]|
|add value |$hash2            |to        |c   |
|check     |value             |c[1].map.a|1   |
|          |Add a map to new list             |
|add value |$hash2            |to        |d   |
|add value |!{mykey:1, key2:2}|to        |d   |
|check     |size of           |d         |2   |
|check     |value             |d[1].mykey|1   |
|check     |value             |d[1].key2 |2   |
|$hash7=   |copy map                          |

|script   |map fixture                |
|         |Add to existing list       |
|add value|6    |to  |a |in    |$hash7|
|check    |value|a[3]|in|$hash7|6     |
|         |Add to new list            |
|add value|Hello|to  |e |in    |$hash7|
|add value|Bye  |to  |e |in    |$hash7|
|check    |value|e[1]|in|$hash7|Bye   |

!2 Copying of values

Copying values is not the same as changing existing map (but nested values are shared)!

|script          |map fixture                        |
|copy values from|$hash1                             |
|check           |value|a         |1                 |
|check           |value|aliases[0]|!-mytest@hsac.nl-!|
|set value       |Dag  |for       |aliases[0]        |
|check           |value|aliases[0]|Dag               |
|set value       |2    |for       |a                 |
|check           |value|a         |in  |$hash1  |1   |
|check           |value|aliases[0]|in  |$hash1  |Dag |
|$hash8=         |copy map                           |

Copy overwrites existing values.

|script          |map fixture                    |
|copy values from|$hash8|to        |$hash1       |
|check           |value |aliases[0]|in|$hash1|Dag|








