├── .formatter.exs ├── .github └── workflows │ └── elixir.yml ├── .gitignore ├── .iex.exs ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── dev └── support │ ├── generators.ex │ ├── trees.ex │ └── zippers.ex ├── lib ├── ex_rose_tree.ex └── ex_rose_tree │ ├── util.ex │ ├── zipper.ex │ └── zipper │ └── location.ex ├── mix.exs ├── mix.lock └── test ├── ex_rose_tree ├── util_test.exs ├── zipper │ ├── direct_ancestor_test.exs │ ├── direct_descendant_test.exs │ ├── extended_cousin_test.exs │ ├── first_cousin_test.exs │ ├── location_test.exs │ ├── nibling_test.exs │ ├── pibling_test.exs │ ├── second_cousin_test.exs │ ├── sibling_test.exs │ └── traversal_test.exs └── zipper_test.exs ├── ex_rose_tree_test.exs ├── support ├── ex_rose_tree_case.ex └── zipper_case.ex └── test_helper.exs /.formatter.exs: -------------------------------------------------------------------------------- 1 | # Used by "mix format" 2 | [ 3 | inputs: ["{mix,.formatter}.exs", "{config,lib,test,dev}/**/*.{ex,exs}"] 4 | ] 5 | -------------------------------------------------------------------------------- /.github/workflows/elixir.yml: -------------------------------------------------------------------------------- 1 | # This workflow uses actions that are not certified by GitHub. 2 | # They are provided by a third-party and are governed by 3 | # separate terms of service, privacy policy, and support 4 | # documentation. 5 | 6 | name: Elixir CI 7 | 8 | on: 9 | push: 10 | branches: [ "main" ] 11 | pull_request: 12 | branches: [ "main" ] 13 | 14 | permissions: 15 | contents: read 16 | 17 | jobs: 18 | mix_test: 19 | name: mix test (OTP ${{matrix.otp}} | Elixir ${{matrix.elixir}}) 20 | 21 | env: 22 | MIX_ENV: test 23 | 24 | strategy: 25 | matrix: 26 | include: 27 | - elixir: 1.14.2 28 | otp: 25.1 29 | lint: true 30 | installer: true 31 | 32 | runs-on: ubuntu-20.04 33 | 34 | steps: 35 | - name: Checkout 36 | uses: actions/checkout@v3 37 | 38 | - name: Set up Elixir 39 | uses: erlef/setup-beam@v1 40 | with: 41 | elixir-version: ${{ matrix.elixir }} 42 | otp-version: ${{ matrix.otp }} 43 | 44 | - name: Restore deps and _build cache 45 | uses: actions/cache@v3 46 | with: 47 | path: | 48 | deps 49 | _build 50 | key: deps-${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('**/mix.lock') }} 51 | restore-keys: | 52 | deps-${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }} 53 | - name: Install dependencies 54 | run: mix deps.get --only test 55 | 56 | - name: Remove compiled application files 57 | run: mix clean 58 | 59 | - name: Compile & lint dependencies 60 | run: mix compile --warnings-as-errors 61 | if: ${{ matrix.lint }} 62 | 63 | - name: Run tests 64 | run: mix test 65 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where third-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | # Also ignore archive artifacts (built via "mix archive.build"). 20 | *.ez 21 | 22 | # Ignore package tarball (built via "mix hex.build"). 23 | ex_rose_tree-*.tar 24 | 25 | # Temporary files, for example, from tests. 26 | /tmp/ 27 | 28 | /config/*.secret.exs 29 | .elixir_ls/ -------------------------------------------------------------------------------- /.iex.exs: -------------------------------------------------------------------------------- 1 | require ExRoseTree 2 | require ExRoseTree.Zipper 3 | 4 | alias ExRoseTree.{Util, Zipper} 5 | alias ExRoseTree.Zipper.Location 6 | alias ExRoseTree.Support.{Generators, Trees, Zippers} 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## CHANGELOG -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality. 6 | 7 | Examples of unacceptable behavior by participants include: 8 | 9 | * The use of sexualized language or imagery 10 | * Personal attacks 11 | * Trolling or insulting/derogatory comments 12 | * Public or private harassment 13 | * Publishing other's private information, such as physical or electronic addresses, without explicit permission 14 | * Other unethical or unprofessional conduct. 15 | 16 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. 17 | 18 | This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. 19 | 20 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 21 | 22 | This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org), version 1.2.0, available at [https://www.contributor-covenant.org/version/1/2/0/code-of-conduct/](https://www.contributor-covenant.org/version/1/2/0/code-of-conduct/) -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to ExRoseTree 2 | 3 | Please take a moment to review this document in order to make the contribution 4 | process easy and effective for everyone involved! 5 | Also make sure you read our [Code of Conduct](CODE_OF_CONDUCT.md) that outlines our 6 | commitment towards an open and welcoming environment. 7 | 8 | ## Using the issue tracker 9 | 10 | Use the issues tracker for: 11 | 12 | * [Bug reports](#bug-reports) 13 | * [Feature requests](#feature-requests) 14 | * [Submitting pull requests](#pull-requests) 15 | 16 | ## Bug reports 17 | 18 | A bug is either a _demonstrable problem_ that is caused by the code in the repository, 19 | or indicate missing, unclear, or misleading documentation. Good bug reports are extremely 20 | helpful - thank you! 21 | 22 | Guidelines for bug reports: 23 | 24 | 1. **Use the GitHub issue search** — check if the issue has already been 25 | reported. 26 | 27 | 2. **Check if the issue has been fixed** — try to reproduce it using the 28 | `main` branch in the repository. 29 | 30 | 3. **Isolate and report the problem** — ideally create a reduced test 31 | case. 32 | 33 | Please try to be as detailed as possible in your report. Include information about 34 | your Operating System, as well as your Erlang, Elixir and ExRoseTree versions. Please 35 | provide steps to reproduce the issue as well as the outcome you were expecting! All 36 | these details will help developers to fix any potential bugs. 37 | 38 | Example: 39 | 40 | > Short and descriptive example bug report title 41 | > 42 | > A summary of the issue and the environment in which it occurs. If suitable, 43 | > include the steps required to reproduce the bug. 44 | > 45 | > 1. This is the first step 46 | > 2. This is the second step 47 | > 3. Further steps, etc. 48 | > 49 | > `` - a link to the reduced test case (e.g. a GitHub Gist) 50 | > 51 | > Any other information you want to share that is relevant to the issue being 52 | > reported. This might include the lines of code that you have identified as 53 | > causing the bug, and potential solutions (and your opinions on their 54 | > merits). 55 | 56 | ## Feature requests 57 | 58 | Feature requests are welcome and may be proposed in the issues tracker. But 59 | take a moment to find out whether your idea fits with the scope and aims of 60 | the project. It's up to *you* to make a strong case to convince the community 61 | of the merits of this feature. Please provide as much detail and context as 62 | possible. 63 | 64 | ## Contributing Documentation 65 | 66 | Code documentation (`@doc`, `@moduledoc`, `@typedoc`) has a special convention: 67 | the first paragraph is considered to be a short summary. Furthermore, always 68 | include a typespec, and if it shares relevance to a group of functions already 69 | part of an `ex_doc` group, make sure you annotate it accordingly. All three 70 | conventions can be seen in the below example. 71 | 72 | For functions, macros and callbacks say what it will do. For example write 73 | something like: 74 | 75 | ```elixir 76 | @doc """ 77 | Returns the current focus of the Zipper. 78 | ... 79 | """ 80 | @doc section: :basic 81 | @spec current_focus(t()) :: ExRoseTree.t() 82 | def current_focus(%__MODULE__{focus: focus}), 83 | do: focus 84 | ``` 85 | 86 | For modules, protocols and types say what it is. For example write 87 | something like: 88 | 89 | ```elixir 90 | defmodule ExRoseTree.Zipper do 91 | @moduledoc """ 92 | A context-aware zipper for advanced traversal and manipulation of an `ExRoseTree`. 93 | ... 94 | """ 95 | ``` 96 | 97 | Keep in mind that the first paragraph might show up in a summary somewhere, long 98 | texts in the first paragraph create very ugly summaries. As a rule of thumb 99 | anything longer than 80 characters is too long. 100 | 101 | Try to keep unnecessary details out of the first paragraph, it's only there to 102 | give a user a quick idea of what the documented "thing" does/is. The rest of the 103 | documentation string can contain the details, for example when a value and when 104 | `nil` is returned. 105 | 106 | If possible include examples, preferably in a form that works with doctests. 107 | This makes it easy to test the examples so that they don't go stale and examples 108 | are often a great help in explaining what a function does. 109 | 110 | ## Pull requests 111 | 112 | Good pull requests - patches, improvements, new features - are a fantastic 113 | help. They should remain focused in scope and avoid containing unrelated 114 | commits. 115 | 116 | **IMPORTANT**: By submitting a patch, you agree that your work will be 117 | licensed under the license used by the project. 118 | 119 | If you have any large pull request in mind (e.g. implementing features, 120 | refactoring code, etc), **please ask first** otherwise you risk spending 121 | a lot of time working on something that the project's developers might 122 | not want to merge into the project. 123 | 124 | Please adhere to the coding conventions in the project (indentation, 125 | accurate comments, etc.) and don't forget to add your own tests and 126 | documentation. When working with git, we recommend the following process 127 | in order to craft an excellent pull request: 128 | 129 | 1. [Fork](https://help.github.com/articles/fork-a-repo/) the project, clone your fork, 130 | and configure the remotes: 131 | 132 | ```bash 133 | # Clone your fork of the repo into the current directory 134 | git clone https://github.com//phoenix 135 | 136 | # Navigate to the newly cloned directory 137 | cd phoenix 138 | 139 | # Assign the original repo to a remote called "upstream" 140 | git remote add upstream https://github.com/phoenixframework/phoenix 141 | ``` 142 | 143 | 2. If you cloned a while ago, get the latest changes from upstream, and update your fork: 144 | 145 | ```bash 146 | git checkout main 147 | git pull upstream main 148 | git push 149 | ``` 150 | 151 | 3. Create a new topic branch (off of `main`) to contain your feature, change, 152 | or fix. 153 | 154 | **IMPORTANT**: Making changes in `main` is discouraged. You should always 155 | keep your local `main` in sync with upstream `main` and make your 156 | changes in topic branches. 157 | 158 | ```bash 159 | git checkout -b 160 | ``` 161 | 162 | 4. Commit your changes in logical chunks. Keep your commit messages organized, 163 | with a short description in the first line and more detailed information on 164 | the following lines. Feel free to use Git's 165 | [interactive rebase](https://help.github.com/articles/about-git-rebase/) 166 | feature to tidy up your commits before making them public. 167 | 168 | 5. Make sure all the tests are still passing. 169 | 170 | ```bash 171 | mix test 172 | ``` 173 | 174 | 6. Push your topic branch up to your fork: 175 | 176 | ```bash 177 | git push origin 178 | ``` 179 | 180 | 7. [Open a Pull Request](https://help.github.com/articles/about-pull-requests/) 181 | with a clear title and description. 182 | 183 | 8. If you haven't updated your pull request for a while, you should consider 184 | rebasing on main and resolving any conflicts. 185 | 186 | **IMPORTANT**: _Never ever_ merge upstream `main` into your branches. You 187 | should always `git rebase` on `main` to bring your changes up to date when 188 | necessary. 189 | 190 | ```bash 191 | git checkout main 192 | git pull upstream main 193 | git checkout 194 | git rebase main 195 | ``` 196 | 197 | Thank you for your contributions! 198 | 199 | ## Guides 200 | 201 | These Guides aim to be inclusive. We use "we" and "our" instead of "you" and 202 | "your" to foster this sense of inclusion. 203 | 204 | Ideally there is something for everybody in each guide, from beginner to expert. 205 | This is hard, maybe impossible. When we need to compromise, we do so on behalf 206 | of beginning users because expert users have more tools at their disposal to 207 | help themselves. 208 | 209 | The general pattern we use for presenting information is to first introduce a 210 | small, discrete topic, then write a small amount of code to demonstrate the 211 | concept, then verify that the code worked. 212 | 213 | In this way, we build from small, easily digestible concepts into more complex 214 | ones. The shorter this cycle is, as long as the information is still clear and 215 | complete, the better. 216 | 217 | For formatting the guides: 218 | 219 | - We use the `elixir` code fence for all module code. 220 | - We use the `iex` for IEx sessions. 221 | - We use the `bash` code fence for shell commands. 222 | - We use the `html` code fence for html templates, even if there is elixir code 223 | in the template. 224 | - We use backticks for filenames and directory paths. 225 | - We use backticks for module names, function names, and variable names. 226 | - Documentation line length should hard wrapped at around 100 characters if possible. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## ExRoseTree :: A Rose Tree and Zipper in Elixir with a slew of navigation primitives. 2 | 3 | [![Build Status](https://github.com/StoatPower/ex-rose-tree/actions/workflows/elixir.yml/badge.svg)](https://github.com/StoatPower/ex-rose-tree/actions/workflows/elixir.yml) 4 | 5 | ### Documentation 6 | 7 | API documentation is available at https://hexdocs.pm/ex_rose_tree. 8 | 9 | 10 | 11 | A Rose Tree with Functional Zipper in Elixir 12 | 13 | ### What's a Rose Tree? 14 | 15 | A [Rose Tree](https://en.wikipedia.org/wiki/Rose_tree), also known as a multi-way or m-way tree 16 | by some, is a general-purpose, recursively defined data structure where each position can have an 17 | an arbitrary value and an arbitrary number of children. Each child is, itself, another Rose Tree. 18 | 19 | ExRoseTree is implemented as a very simple struct `defstruct ~w(term children)a` with an equally 20 | simple typespec: 21 | 22 | ```elixir 23 | @type t() :: %__MODULE__{ 24 | term: term(), 25 | children: [t()] 26 | } 27 | ``` 28 | 29 | ### What's it good for? 30 | 31 | Practically speaking, a few good use cases for Rose Trees could be: 32 | 33 | * outlines 34 | * file systems 35 | * parsing HTML/XML documents 36 | * [abstract syntax trees](https://en.wikipedia.org/wiki/Abstract_syntax_tree) 37 | * decision trees 38 | * data visualization (org charts, family trees, taxonomies, nested menus) 39 | 40 | This implementation also comes with a companion `ExRoseTree.Zipper` data structure, and greatly 41 | enhances the usefulness of the standard Rose Tree. 42 | 43 | ### So what's a Zipper? 44 | 45 | A [Zipper](https://en.wikipedia.org/wiki/Zipper_(data_structure)) of a given data structure can 46 | be thought of as taking the derivative of that data structure. It provides an efficient, context-aware 47 | approach to traversing and manipulating the contents of the Rose Tree. 48 | 49 | In his foundational [paper](https://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf) 50 | formalizing the idea, Gerard Huet perhaps describes it best: 51 | 52 | > The basic idea is simple: the tree is turned inside-out like a returned glove, 53 | > pointers from the root to the current position being reversed in a path structure. The 54 | > current location holds both the downward current subtree and the upward path. All 55 | > navigation and modification primitives operate on the location structure. Going up 56 | > and down in the structure is analogous to closing and opening a zipper in a piece 57 | > of clothing, whence the name. 58 | 59 | ### And what's a Zipper of a Rose Tree good for? 60 | 61 | In practice, `ExRoseTree.Zipper` can be used as an effective means of representing everything from a cursor 62 | in a text editor to a selected item in a nested sidebar/dropdown menu in a UI which needs to maintain persistent 63 | focus. Essentially, anything that has an arbitrary hierarchy and would necessitate or benefit from the capability of 64 | being context-aware could be a candidate for a Rose Tree with Zipper. 65 | 66 | ## Installation 67 | 68 | If [available in Hex](https://hex.pm/docs/publish), the package can be installed 69 | by adding `ex_rose_tree` to your list of dependencies in `mix.exs`: 70 | 71 | ```elixir 72 | def deps do 73 | [ 74 | {:ex_rose_tree, "~> 0.1.0"} 75 | ] 76 | end 77 | ``` 78 | 79 | ## Example Usage 80 | 81 | ```elixir 82 | alias ExRoseTree, as: Tree 83 | alias ExRoseTree.Zipper 84 | 85 | Tree.new(1) 86 | # %ExRoseTree{term: 1, children: []} 87 | 88 | tree = Tree.new(1, [2,3,4,5]) 89 | # %ExRoseTree{term: 1, children: [ 90 | # %ExRoseTree{term: 2, children: []}, 91 | # %ExRoseTree{term: 3, children: []}, 92 | # %ExRoseTree{term: 4, children: []}, 93 | # %ExRoseTree{term: 5, children: []}, 94 | # ]} 95 | 96 | zipper = Zipper.new(tree) 97 | # %ExRoseTree.Zipper{ 98 | # focus: %ExRoseTree{ 99 | # term: 1, 100 | # children: [ 101 | # %ExRoseTree{term: 2, children: []}, 102 | # %ExRoseTree{term: 3, children: []}, 103 | # %ExRoseTree{term: 4, children: []}, 104 | # %ExRoseTree{term: 5, children: []} 105 | # ] 106 | # }, 107 | # prev: [], 108 | # next: [], 109 | # path: [] 110 | # } 111 | 112 | zipper = Zipper.last_child(zipper) 113 | # %ExRoseTree.Zipper{ 114 | # focus: %ExRoseTree{term: 5, children: []}, 115 | # prev: [ 116 | # %ExRoseTree{term: 4, children: []}, 117 | # %ExRoseTree{term: 3, children: []}, 118 | # %ExRoseTree{term: 2, children: []} 119 | # ], 120 | # next: [], 121 | # path: [%ExRoseTree.Zipper.Location{prev: [], term: 1, next: []}] 122 | # } 123 | 124 | zipper = Zipper.backward(zipper) 125 | # %ExRoseTree.Zipper{ 126 | # focus: %ExRoseTree{term: 4, children: []}, 127 | # prev: [ 128 | # %ExRoseTree{term: 3, children: []}, 129 | # %ExRoseTree{term: 2, children: []} 130 | # ], 131 | # next: [%ExRoseTree{term: 5, children: []}], 132 | # path: [%ExRoseTree.Zipper.Location{prev: [], term: 1, next: []}] 133 | # } 134 | 135 | zipper = Zipper.rewind_map(zipper, &Tree.map_term(&1, fn t -> t * 10 end)) 136 | # %ExRoseTree.Zipper{ 137 | # focus: %ExRoseTree{ 138 | # term: 10, 139 | # children: [ 140 | # %ExRoseTree{term: 2, children: []}, 141 | # %ExRoseTree{term: 3, children: []}, 142 | # %ExRoseTree{term: 40, children: []}, 143 | # %ExRoseTree{term: 5, children: []}, 144 | # ] 145 | # }, 146 | # prev: [], 147 | # next: [], 148 | # path: [] 149 | # } 150 | 151 | Zipper.to_tree(zipper) 152 | # %ExRoseTree{ 153 | # term: 10, 154 | # children: [ 155 | # %ExRoseTree{term: 2, children: []}, 156 | # %ExRoseTree{term: 3, children: []}, 157 | # %ExRoseTree{term: 40, children: []}, 158 | # %ExRoseTree{term: 5, children: []} 159 | # ] 160 | # } 161 | ``` 162 | 163 | ## Testing and Disclaimer 164 | 165 | While great pains have been taken to provide extensive test coverage--over 800 166 | tests at present, this library is still pre-1.0, so be sure to do your due diligence 167 | for your own use case. 168 | 169 | To run the test suite: 170 | 171 | ```bash 172 | $ mix deps.get 173 | $ mix test 174 | ``` 175 | 176 | To run test coverage with [excoveralls](https://github.com/parroty/excoveralls): 177 | 178 | ```bash 179 | $ mix deps.get 180 | $ mix coveralls 181 | ``` 182 | 183 | or for HTML output: 184 | 185 | ```bash 186 | $ mix deps.get 187 | $ MIX_ENV=test mix coveralls.html 188 | ``` 189 | 190 | 191 | 192 | ## Contributions, Issues, and Further Development 193 | 194 | Additional functionality and work to explore adding include: 195 | 196 | * Tree diffing and merging algorithms 197 | * Multiple cursor support (i.e.: multiple, concurrent contexts on a Zipper) 198 | * LiveBook examples 199 | * Visualizations of the many traversal functions 200 | * Improvements to the generators 201 | * Even more unit tests, including property tests 202 | * Performance improvements and benchmarks 203 | * Change tracking and pluggable backends for persistence 204 | * Documentation, guide, and example improvement, clarification, and cohesion 205 | 206 | We're open to any and all ideas and thoughtful [contribution](/CONTRIBUTING.md) here, 207 | so don't hesitate to pipe in, but please be sure to follow our [Code of Conduct](/CODE_OF_CONDUCT.md). 208 | If you find a bug or it doesn't quite meet your needs without feature X, consider 209 | opening an issue in the issues tracker. 210 | 211 | ## Thanks 212 | 213 | A big thanks to [@zwilias](https://github.com/zwilias) and his Elm package 214 | [elm-rosetree](https://github.com/zwilias/elm-rosetree/tree/1.5.0) for the 215 | initial inspiration in building this library. 216 | 217 | Continued thanks to [@odoood](https://github.com/odoood) for ongoing help reviewing code and improving documentation. 218 | 219 | ## Copyright and License 220 | 221 | Copyright (c) 2022-present, Paraclade, LLC. 222 | 223 | ExRoseTree source code is licensed under the [Apache License 2.0](/LICENSE). 224 | 225 | 226 | 227 | -------------------------------------------------------------------------------- /dev/support/generators.ex: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Support.Generators do 2 | require Logger 3 | 4 | alias ExRoseTree.{Util, Zipper} 5 | alias ExRoseTree.Zipper.Location 6 | 7 | @typep default_seed() :: %{ 8 | current_depth: non_neg_integer(), 9 | num_children: non_neg_integer(), 10 | shares_for_children: non_neg_integer() 11 | } 12 | 13 | @spec random_tree(keyword()) :: ExRoseTree.t() 14 | def random_tree(options \\ []) do 15 | {initial_seed, unfolder_fn} = default_init(options) 16 | 17 | ExRoseTree.unfold(initial_seed, unfolder_fn) 18 | end 19 | 20 | @spec random_zipper(keyword()) :: Zipper.t() 21 | def random_zipper(options \\ []) do 22 | focus = random_tree(options) 23 | 24 | %Zipper{ 25 | focus: focus, 26 | prev: [], 27 | next: [], 28 | path: [] 29 | } 30 | |> add_zipper_siblings(options) 31 | |> add_zipper_locations(options) 32 | end 33 | 34 | @spec add_zipper_siblings(Zipper.t(), keyword()) :: Zipper.t() 35 | def add_zipper_siblings(%Zipper{} = z, options \\ []) do 36 | num_siblings = Keyword.get(options, :num_siblings, Enum.random(0..5)) 37 | 38 | if num_siblings == 0 do 39 | z 40 | else 41 | random_trees = for _ <- 0..(num_siblings - 1), do: random_tree(options) 42 | 43 | {prev, next} = 44 | random_trees 45 | |> Util.split_at(Enum.random(0..(num_siblings - 1))) 46 | 47 | %Zipper{z | prev: prev, next: next} 48 | end 49 | end 50 | 51 | @spec add_zipper_locations(Zipper.t(), keyword()) :: Zipper.t() 52 | def add_zipper_locations(%Zipper{} = z, options \\ []) do 53 | num_locations = Keyword.get(options, :num_locations, Enum.random(0..5)) 54 | 55 | if num_locations == 0 do 56 | z 57 | else 58 | random_locations = 59 | for _ <- 0..(num_locations - 1) do 60 | z = random_zipper(num_locations: 0) 61 | %Location{prev: z.prev, term: z.focus.term, next: z.next} 62 | end 63 | 64 | %Zipper{z | path: random_locations} 65 | end 66 | end 67 | 68 | @spec default_unfolder(default_seed(), non_neg_integer()) :: {pos_integer(), [default_seed()]} 69 | def default_unfolder(seed, max_children) do 70 | range = 10_000 71 | 72 | case seed do 73 | # stop if we run out of total remaining seeds 74 | %{current_depth: _current_depth, num_children: num_children, shares_for_children: _} 75 | when num_children <= 0 -> 76 | {:rand.uniform(range), []} 77 | 78 | %{ 79 | current_depth: current_depth, 80 | num_children: num_children, 81 | shares_for_children: shares_for_children 82 | } -> 83 | new_depth = current_depth + 1 84 | 85 | {new_seeds, remaining_shares} = 86 | 1..num_children 87 | |> Enum.reduce({[], shares_for_children}, fn 88 | _, {new_children, remaining_shares} when remaining_shares <= 0 -> 89 | new_child = new_seed(new_depth, 0, 0) 90 | {[new_child | new_children], 0} 91 | 92 | _, {new_children, remaining_shares} -> 93 | num_grandchildren = :rand.uniform(remaining_shares) 94 | 95 | num_grandchildren = 96 | if num_grandchildren > max_children do 97 | max_children 98 | else 99 | num_grandchildren 100 | end 101 | 102 | new_child = new_seed(new_depth, num_grandchildren, 0) 103 | {[new_child | new_children], remaining_shares - num_grandchildren} 104 | end) 105 | 106 | new_seeds = 107 | new_seeds 108 | |> allot_remaining_shares(remaining_shares) 109 | |> Enum.shuffle() 110 | 111 | {:rand.uniform(range), new_seeds} 112 | end 113 | end 114 | 115 | @spec allot_remaining_shares([default_seed()], non_neg_integer()) :: [default_seed()] 116 | def allot_remaining_shares(seeds, shares) do 117 | do_allot_remaining_shares([], seeds, shares) 118 | end 119 | 120 | defp do_allot_remaining_shares(processed, todo, shares) when shares <= 0, 121 | do: processed ++ todo 122 | 123 | defp do_allot_remaining_shares(processed, [] = _todo, shares), 124 | do: do_allot_remaining_shares([], processed, shares) 125 | 126 | defp do_allot_remaining_shares(processed, [seed | seeds] = _todo, shares) do 127 | allotted = :rand.uniform(shares) 128 | 129 | [%{seed | shares_for_children: seed.shares_for_children + allotted} | processed] 130 | |> do_allot_remaining_shares(seeds, shares - allotted) 131 | end 132 | 133 | @spec default_init(keyword()) :: {default_seed(), ExRoseTree.unfold_fn()} 134 | def default_init(options \\ []) do 135 | total_nodes = Keyword.get(options, :total_nodes, random_number_of_nodes()) 136 | 137 | max_children = Keyword.get(options, :max_children, total_nodes - 1) 138 | 139 | root_children = 140 | if max_children == 0 do 141 | 0 142 | else 143 | :rand.uniform(max_children) 144 | end 145 | 146 | initial_seed = new_seed(0, root_children, total_nodes - root_children - 1) 147 | 148 | unfolder_fn = &default_unfolder(&1, max_children) 149 | 150 | {initial_seed, unfolder_fn} 151 | end 152 | 153 | @spec new_seed(non_neg_integer(), non_neg_integer(), non_neg_integer()) :: default_seed() 154 | def new_seed(current_depth, num_children, shares_for_children) do 155 | %{ 156 | current_depth: current_depth, 157 | num_children: num_children, 158 | shares_for_children: shares_for_children 159 | } 160 | end 161 | 162 | @spec random_number_of_nodes() :: 1..100 163 | def random_number_of_nodes(), do: :rand.uniform(100) 164 | end 165 | -------------------------------------------------------------------------------- /dev/support/trees.ex: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Support.Trees do 2 | @moduledoc """ 3 | Sample ExRoseTrees for use in development and testing. 4 | """ 5 | 6 | def empty_tree() do 7 | %ExRoseTree{term: nil, children: []} 8 | end 9 | 10 | def leaf_tree() do 11 | %ExRoseTree{term: 1, children: []} 12 | end 13 | 14 | def simple_tree() do 15 | %ExRoseTree{ 16 | term: 1, 17 | children: [ 18 | %ExRoseTree{term: 2, children: []}, 19 | %ExRoseTree{term: 3, children: []}, 20 | %ExRoseTree{term: 4, children: []} 21 | ] 22 | } 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /lib/ex_rose_tree.ex: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree do 2 | 3 | @moduledoc File.read!(Path.expand("README.md")) 4 | |> String.split("") 5 | |> Enum.at(1) 6 | |> String.split("") 7 | |> List.first() 8 | 9 | defstruct ~w(term children)a 10 | 11 | @typedoc """ 12 | The foundational, recursive data type of an `ExRoseTree`. 13 | 14 | * `term` can by any valid Erlang `term()` 15 | * `children` is a list of `t()` 16 | """ 17 | @type t() :: %__MODULE__{ 18 | term: term(), 19 | children: [t()] 20 | } 21 | 22 | @type predicate() :: (t() -> boolean()) 23 | 24 | ### 25 | ### GUARDS 26 | ### 27 | 28 | @doc section: :guards 29 | defguard rose_tree?(value) 30 | when is_struct(value) and value.__struct__ == __MODULE__ and is_list(value.children) 31 | 32 | @doc section: :guards 33 | defguard empty?(value) when rose_tree?(value) and value.term == nil and value.children == [] 34 | 35 | @doc section: :guards 36 | defguard leaf?(value) when rose_tree?(value) and value.children == [] 37 | 38 | @doc section: :guards 39 | defguard parent?(value) 40 | when rose_tree?(value) and is_list(value.children) and value.children != [] 41 | 42 | ### 43 | ### BASIC 44 | ### 45 | 46 | @doc """ 47 | Initializes an empty tree. 48 | 49 | ## Examples 50 | 51 | iex> ExRoseTree.empty() 52 | %ExRoseTree{term: nil, children: []} 53 | 54 | """ 55 | @doc section: :basic 56 | @spec empty() :: t() 57 | def empty(), do: %__MODULE__{term: nil, children: []} 58 | 59 | @doc """ 60 | Initializes a new tree with the given `term`. 61 | 62 | ## Examples 63 | 64 | iex> ExRoseTree.new("new term") 65 | %ExRoseTree{term: "new term", children: []} 66 | 67 | iex> ExRoseTree.new(5, [4, 3, 2, 1]) 68 | %ExRoseTree{ 69 | term: 5, 70 | children: [ 71 | %ExRoseTree{term: 4, children: []}, 72 | %ExRoseTree{term: 3, children: []}, 73 | %ExRoseTree{term: 2, children: []}, 74 | %ExRoseTree{term: 1, children: []} 75 | ] 76 | } 77 | 78 | iex> children = [ 79 | ...> %ExRoseTree{term: 4, children: []}, 80 | ...> 3, 81 | ...> %ExRoseTree{term: 2, children: []}, 82 | ...> %ExRoseTree{term: 1, children: []} 83 | ...> ] 84 | ...> ExRoseTree.new(5, children) 85 | %ExRoseTree{ 86 | term: 5, 87 | children: [ 88 | %ExRoseTree{term: 4, children: []}, 89 | %ExRoseTree{term: 3, children: []}, 90 | %ExRoseTree{term: 2, children: []}, 91 | %ExRoseTree{term: 1, children: []} 92 | ] 93 | } 94 | 95 | """ 96 | @doc section: :basic 97 | @spec new(term(), [t() | term()]) :: t() 98 | def new(term, children \\ []) 99 | 100 | def new(term, []), do: %__MODULE__{term: term, children: []} 101 | 102 | def new(term, children) when is_list(children) do 103 | new_children = 104 | children 105 | |> Enum.map(fn 106 | child when rose_tree?(child) -> 107 | child 108 | 109 | child -> 110 | new(child) 111 | end) 112 | 113 | %__MODULE__{ 114 | term: term, 115 | children: new_children 116 | } 117 | end 118 | 119 | @doc """ 120 | Returns whether a list of values are all `ExRoseTree`s or not. Will return 121 | `true` if passed an empty list. 122 | 123 | ## Examples 124 | 125 | iex> trees = for t <- [5,4,3,2,1], do: ExRoseTree.new(t) 126 | ...> ExRoseTree.all_rose_trees?(trees) 127 | true 128 | 129 | """ 130 | @doc section: :basic 131 | @spec all_rose_trees?([term()]) :: boolean() 132 | def all_rose_trees?(values) when is_list(values) do 133 | Enum.all?(values, &rose_tree?(&1)) 134 | end 135 | 136 | ### 137 | ### TERM 138 | ### 139 | 140 | @doc """ 141 | Returns the inner `term` of an `ExRoseTree`. 142 | 143 | ## Examples 144 | 145 | iex> tree = ExRoseTree.new(5) 146 | ...> ExRoseTree.get_term(tree) 147 | 5 148 | 149 | """ 150 | @doc section: :term 151 | @spec get_term(t()) :: term() 152 | def get_term(tree) when rose_tree?(tree), do: tree.term 153 | 154 | @doc """ 155 | Sets the tree `term` to the given `term`. 156 | 157 | ## Examples 158 | 159 | iex> tree = ExRoseTree.new(5) 160 | ...> ExRoseTree.set_term(tree, "five") 161 | %ExRoseTree{term: "five", children: []} 162 | 163 | """ 164 | @doc section: :term 165 | @spec set_term(t(), term()) :: t() 166 | def set_term(%__MODULE__{} = tree, term) do 167 | %{tree | term: term} 168 | end 169 | 170 | @doc """ 171 | Applies the given function to the tree `term`. 172 | 173 | ## Examples 174 | 175 | iex> tree = ExRoseTree.new(5) 176 | ...> ExRoseTree.map_term(tree, fn x -> x * 2 end) 177 | %ExRoseTree{term: 10, children: []} 178 | 179 | """ 180 | @doc section: :term 181 | @spec map_term(t(), (term() -> term())) :: t() 182 | def map_term(%__MODULE__{term: term} = tree, map_fn) 183 | when is_function(map_fn) do 184 | new_term = map_fn.(term) 185 | 186 | %{tree | term: new_term} 187 | end 188 | 189 | ### 190 | ### CHILDREN 191 | ### 192 | 193 | @doc """ 194 | Returns whether or not the current `tree` has a child that matches the `predicate`. 195 | 196 | ## Examples 197 | 198 | iex> tree = ExRoseTree.new(5, [4,3,2,1]) 199 | ...> ExRoseTree.has_child?(tree, &(&1.term == 2)) 200 | true 201 | 202 | """ 203 | @doc section: :children 204 | @spec has_child?(t(), (t() -> boolean())) :: boolean() 205 | def has_child?(%__MODULE__{children: children} = _tree, predicate) when is_function(predicate) do 206 | Enum.any?(children, predicate) 207 | end 208 | 209 | @doc """ 210 | Returns the children of an `ExRoseTree`. 211 | 212 | ## Examples 213 | 214 | iex> tree = ExRoseTree.new(5, [4,3,2,1]) 215 | ...> ExRoseTree.get_children(tree) 216 | [ 217 | %ExRoseTree{term: 4, children: []}, 218 | %ExRoseTree{term: 3, children: []}, 219 | %ExRoseTree{term: 2, children: []}, 220 | %ExRoseTree{term: 1, children: []} 221 | ] 222 | 223 | """ 224 | @doc section: :children 225 | @spec get_children(t()) :: [t()] 226 | def get_children(tree) when rose_tree?(tree), do: tree.children 227 | 228 | @doc """ 229 | Sets the `tree` children to the given list of `children`. 230 | 231 | ## Examples 232 | 233 | iex> tree = ExRoseTree.new(5) 234 | ...> ExRoseTree.set_children(tree, [4, 3, 2, 1]) 235 | %ExRoseTree{ 236 | term: 5, 237 | children: [ 238 | %ExRoseTree{term: 4, children: []}, 239 | %ExRoseTree{term: 3, children: []}, 240 | %ExRoseTree{term: 2, children: []}, 241 | %ExRoseTree{term: 1, children: []} 242 | ] 243 | } 244 | 245 | """ 246 | @doc section: :children 247 | @spec set_children(t(), [t() | term()]) :: t() 248 | def set_children(%__MODULE__{} = tree, children) when is_list(children) do 249 | new_children = 250 | children 251 | |> Enum.map(fn 252 | %__MODULE__{} = child -> 253 | child 254 | 255 | child -> 256 | new(child) 257 | end) 258 | 259 | %{tree | children: new_children} 260 | end 261 | 262 | @doc """ 263 | Applies the given function to each child tree. The `map_fn` should 264 | return a valid `ExRoseTree` struct. 265 | 266 | ## Examples 267 | 268 | iex> tree = ExRoseTree.new(5, [4, 3, 2, 1]) 269 | ...> ExRoseTree.map_children(tree, fn child -> 270 | ...> ExRoseTree.map_term(child, fn x -> x * 2 end) 271 | ...> end) 272 | %ExRoseTree{ 273 | term: 5, 274 | children: [ 275 | %ExRoseTree{term: 8, children: []}, 276 | %ExRoseTree{term: 6, children: []}, 277 | %ExRoseTree{term: 4, children: []}, 278 | %ExRoseTree{term: 2, children: []} 279 | ] 280 | } 281 | 282 | """ 283 | @doc section: :children 284 | @spec map_children(t(), (t() -> t())) :: t() 285 | def map_children(%__MODULE__{children: children} = tree, map_fn) 286 | when is_function(map_fn) do 287 | new_children = 288 | children 289 | |> Enum.map(fn child -> map_fn.(child) end) 290 | 291 | if all_rose_trees?(new_children) do 292 | %{tree | children: new_children} 293 | else 294 | raise ArgumentError, "map_fn must return a valid ExRoseTree struct" 295 | end 296 | end 297 | 298 | @doc """ 299 | Prepends the given `child` to the `tree`'s children. 300 | 301 | ## Examples 302 | 303 | iex> tree = ExRoseTree.new(5, [4, 3, 2, 1]) 304 | ...> ExRoseTree.prepend_child(tree, 0) 305 | %ExRoseTree{ 306 | term: 5, 307 | children: [ 308 | %ExRoseTree{term: 0, children: []}, 309 | %ExRoseTree{term: 4, children: []}, 310 | %ExRoseTree{term: 3, children: []}, 311 | %ExRoseTree{term: 2, children: []}, 312 | %ExRoseTree{term: 1, children: []} 313 | ] 314 | } 315 | 316 | """ 317 | @doc section: :children 318 | @spec prepend_child(t(), t() | term()) :: t() 319 | def prepend_child(%__MODULE__{children: children} = tree, child) 320 | when rose_tree?(child) do 321 | %{tree | children: [child | children]} 322 | end 323 | 324 | def prepend_child(%__MODULE__{children: children} = tree, child) do 325 | %{tree | children: [new(child) | children]} 326 | end 327 | 328 | @doc """ 329 | Removes the first child of the `tree`'s children. 330 | 331 | ## Examples 332 | 333 | iex> tree = ExRoseTree.new(5, [4, 3, 2, 1]) 334 | ...> ExRoseTree.pop_first_child(tree) 335 | { 336 | %ExRoseTree{ 337 | term: 5, 338 | children: [ 339 | %ExRoseTree{term: 3, children: []}, 340 | %ExRoseTree{term: 2, children: []}, 341 | %ExRoseTree{term: 1, children: []} 342 | ] 343 | }, %ExRoseTree{term: 4, children: []} 344 | } 345 | 346 | """ 347 | @doc section: :children 348 | @spec pop_first_child(t()) :: {t(), t() | nil} 349 | def pop_first_child(%__MODULE__{children: []} = tree), do: {tree, nil} 350 | 351 | def pop_first_child(%__MODULE__{children: [child | children]} = tree) do 352 | {%{tree | children: children}, child} 353 | end 354 | 355 | @doc """ 356 | Appends the given `child` to the `tree`'s children. 357 | 358 | ## Examples 359 | 360 | iex> tree = ExRoseTree.new(5, [4, 3, 2, 1]) 361 | ...> ExRoseTree.append_child(tree, 0) 362 | %ExRoseTree{ 363 | term: 5, 364 | children: [ 365 | %ExRoseTree{term: 4, children: []}, 366 | %ExRoseTree{term: 3, children: []}, 367 | %ExRoseTree{term: 2, children: []}, 368 | %ExRoseTree{term: 1, children: []}, 369 | %ExRoseTree{term: 0, children: []} 370 | ] 371 | } 372 | 373 | """ 374 | @doc section: :children 375 | @spec append_child(t(), t() | term()) :: t() 376 | def append_child(%__MODULE__{children: children} = tree, child) 377 | when rose_tree?(child) do 378 | %{tree | children: children ++ [child]} 379 | end 380 | 381 | def append_child(%__MODULE__{children: children} = tree, child) do 382 | %{tree | children: children ++ [new(child)]} 383 | end 384 | 385 | @doc """ 386 | Removes the last child of the `tree`'s children. 387 | 388 | ## Examples 389 | 390 | iex> tree = ExRoseTree.new(5, [4, 3, 2, 1]) 391 | ...> ExRoseTree.pop_last_child(tree) 392 | { 393 | %ExRoseTree{ 394 | term: 5, 395 | children: [ 396 | %ExRoseTree{term: 4, children: []}, 397 | %ExRoseTree{term: 3, children: []}, 398 | %ExRoseTree{term: 2, children: []} 399 | ] 400 | }, %ExRoseTree{term: 1, children: []} 401 | } 402 | 403 | """ 404 | @doc section: :children 405 | @spec pop_last_child(t()) :: {t(), t() | nil} 406 | def pop_last_child(%__MODULE__{children: []} = tree), do: {tree, nil} 407 | 408 | def pop_last_child(%__MODULE__{children: children} = tree) do 409 | {new_children, [popped_child | []]} = Enum.split(children, length(children) - 1) 410 | {%{tree | children: new_children}, popped_child} 411 | end 412 | 413 | @doc """ 414 | Inserts a new `child` into the `tree`'s children at the given `index`. 415 | 416 | ## Examples 417 | 418 | iex> tree = ExRoseTree.new(5, [4, 3, 2, 1]) 419 | ...> ExRoseTree.insert_child(tree, 3.5, 2) 420 | %ExRoseTree{ 421 | term: 5, 422 | children: [ 423 | %ExRoseTree{term: 4, children: []}, 424 | %ExRoseTree{term: 3, children: []}, 425 | %ExRoseTree{term: 3.5, children: []}, 426 | %ExRoseTree{term: 2, children: []}, 427 | %ExRoseTree{term: 1, children: []} 428 | ] 429 | } 430 | 431 | """ 432 | @doc section: :children 433 | @spec insert_child(t(), t() | term(), integer()) :: t() 434 | def insert_child(%__MODULE__{} = tree, child, index) when rose_tree?(child), 435 | do: do_insert_child(tree, child, index) 436 | 437 | def insert_child(%__MODULE__{} = tree, child, index), 438 | do: do_insert_child(tree, new(child), index) 439 | 440 | @spec do_insert_child(t(), t() | term(), integer()) :: t() 441 | defp do_insert_child(%__MODULE__{children: children} = tree, child, index) 442 | when rose_tree?(child) and is_integer(index) do 443 | {previous_children, next_children} = Enum.split(children, index) 444 | new_children = previous_children ++ [child | next_children] 445 | %{tree | children: new_children} 446 | end 447 | 448 | @doc """ 449 | Removes a child from the `tree`'s children at the given `index`. 450 | 451 | ## Examples 452 | 453 | iex> tree = ExRoseTree.new(5, [4, 3, 2, 1]) 454 | ...> ExRoseTree.remove_child(tree, 2) 455 | { 456 | %ExRoseTree{ 457 | term: 5, 458 | children: [ 459 | %ExRoseTree{term: 4, children: []}, 460 | %ExRoseTree{term: 3, children: []}, 461 | %ExRoseTree{term: 1, children: []} 462 | ] 463 | }, 464 | %ExRoseTree{term: 2, children: []} 465 | } 466 | 467 | """ 468 | @doc section: :children 469 | @spec remove_child(t(), integer()) :: {t(), t() | nil} 470 | def remove_child(%__MODULE__{children: []} = tree, _index), do: {tree, nil} 471 | 472 | def remove_child(%__MODULE__{children: children} = tree, index) 473 | when is_integer(index) and index < 0 do 474 | if abs(index) > length(children) do 475 | {tree, nil} 476 | else 477 | do_remove_child(tree, index) 478 | end 479 | end 480 | 481 | def remove_child(%__MODULE__{} = tree, index) when is_integer(index), 482 | do: do_remove_child(tree, index) 483 | 484 | @spec do_remove_child(t(), integer()) :: {t(), t() | nil} 485 | defp do_remove_child(%__MODULE__{children: children} = tree, index) when is_integer(index) do 486 | {new_children, removed_child} = 487 | case Enum.split(children, index) do 488 | {previous, []} -> 489 | {previous, nil} 490 | 491 | {previous, [removed | next]} -> 492 | {previous ++ next, removed} 493 | end 494 | 495 | {%{tree | children: new_children}, removed_child} 496 | end 497 | 498 | @typep unfold_acc() :: %{ 499 | current: term(), 500 | todo: [term()], 501 | done: [t()] 502 | } 503 | 504 | @typedoc """ 505 | A function that takes a seed value and returns a new `ExRoseTree` and a 506 | list of new seeds to use for children. Care must be taken that you 507 | don't create a function that infinitely creates new seeds, in 508 | other words, the function should have a terminating base case. 509 | """ 510 | @type unfold_fn() :: (seed :: term() -> {term(), [seed :: term()]}) 511 | 512 | @doc """ 513 | Given a `seed` value and an `unfold_fn()`, generates a new rose tree. 514 | 515 | ## Examples 516 | 517 | iex> unfolder = fn 518 | ...> x when x > 0 -> {Integer.to_string(x), Enum.to_list(0..x-1)} 519 | ...> x -> {Integer.to_string(x), []} 520 | ...> end 521 | ...> ExRoseTree.unfold(3, unfolder) 522 | %ExRoseTree{ 523 | term: "3", 524 | children: [ 525 | %ExRoseTree{term: "0", children: []}, 526 | %ExRoseTree{ 527 | term: "1", 528 | children: [%ExRoseTree{term: "0", children: []}] 529 | }, 530 | %ExRoseTree{ 531 | term: "2", 532 | children: [ 533 | %ExRoseTree{term: "0", children: []}, 534 | %ExRoseTree{ 535 | term: "1", 536 | children: [%ExRoseTree{term: "0", children: []}] 537 | } 538 | ] 539 | } 540 | ] 541 | } 542 | 543 | """ 544 | @doc section: :special 545 | @spec unfold(seed :: term(), unfold_fn()) :: t() 546 | def unfold(seed, unfold_fn) when is_function(unfold_fn) do 547 | {current, next} = unfold_fn.(seed) 548 | 549 | %{current: current, todo: next, done: []} 550 | |> do_unfold(_stack = [], unfold_fn) 551 | end 552 | 553 | @spec do_unfold(unfold_acc(), [term()], unfold_fn()) :: t() 554 | defp do_unfold(%{todo: []} = acc, [] = _stack, unfold_fn) when is_function(unfold_fn), 555 | do: new(acc.current, Enum.reverse(acc.done)) 556 | 557 | defp do_unfold(%{todo: []} = acc, [top | rest] = _stack, unfold_fn) 558 | when is_function(unfold_fn) do 559 | tree = new(acc.current, Enum.reverse(acc.done)) 560 | 561 | %{top | done: [tree | top.done]} 562 | |> do_unfold(rest, unfold_fn) 563 | end 564 | 565 | defp do_unfold(%{todo: [next | rest]} = acc, stack, unfold_fn) 566 | when is_list(stack) and is_function(unfold_fn) do 567 | case unfold_fn.(next) do 568 | {current, []} -> 569 | %{acc | todo: rest, done: [new(current) | acc.done]} 570 | |> do_unfold(stack, unfold_fn) 571 | 572 | {current, todo} -> 573 | %{current: current, todo: todo, done: []} 574 | |> do_unfold([%{acc | todo: rest} | stack], unfold_fn) 575 | end 576 | end 577 | 578 | ## Implement Enumerable Protocol 579 | 580 | defimpl Enumerable do 581 | alias ExRoseTree 582 | 583 | def count(%ExRoseTree{term: nil, children: []}), do: {:ok, 0} 584 | def count(_tree), do: {:error, __MODULE__} 585 | 586 | def member?(%ExRoseTree{term: nil, children: []}, _value), do: {:ok, false} 587 | def member?(_tree, _value), do: {:error, __MODULE__} 588 | 589 | def slice(%ExRoseTree{} = _tree), do: {:error, __MODULE__} 590 | 591 | def reduce(%ExRoseTree{} = tree, acc, fun) do 592 | do_reduce(acc, fun, [tree], []) 593 | end 594 | 595 | defp do_reduce({:halt, acc}, _fun, _trees, _remaining_tree_sets), 596 | do: {:halted, acc} 597 | 598 | defp do_reduce({:suspend, acc}, fun, trees, remaining_tree_sets), 599 | do: {:suspended, acc, &do_reduce(&1, fun, trees, remaining_tree_sets)} 600 | 601 | defp do_reduce({:cont, acc}, _fun, [] = _trees, [] = _remaining_tree_sets), 602 | do: {:done, acc} 603 | 604 | defp do_reduce({:cont, acc}, fun, [] = _trees, [h | t] = _remaining_tree_sets), 605 | do: do_reduce({:cont, acc}, fun, [h], t) 606 | 607 | defp do_reduce( 608 | {:cont, acc}, 609 | fun, 610 | [%ExRoseTree{children: []} = h | t], 611 | remaining_tree_sets 612 | ), 613 | do: do_reduce(fun.(h.term, acc), fun, t, remaining_tree_sets) 614 | 615 | defp do_reduce( 616 | {:cont, acc}, 617 | fun, 618 | [%ExRoseTree{children: children} = h | t], 619 | remaining_tree_sets 620 | ), 621 | do: do_reduce(fun.(h.term, acc), fun, children, t ++ remaining_tree_sets) 622 | end 623 | end 624 | -------------------------------------------------------------------------------- /lib/ex_rose_tree/util.ex: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Util do 2 | @moduledoc """ 3 | Various utility functions. 4 | """ 5 | 6 | @type result() :: {:ok, term()} | {:error, term()} | :error 7 | 8 | @type result_fn() :: (... -> result()) 9 | 10 | @doc """ 11 | Given a term and a list of potential functions to apply to the term, 12 | will return with the result of the first one that succeeds when 13 | applied to the subject term. If no functions succeed, returns nil. 14 | 15 | The list of functions should each be of type `ExRoseTree.Util.result_fn()`, 16 | in other words, they should return a `ExRoseTree.Util.result()` type. 17 | 18 | ## Examples 19 | 20 | iex> funs = for n <- [4,3,2,1], do: &(if n == &1 do {:ok, n} else :error end) 21 | ...> ExRoseTree.Util.first_of(3, funs) 22 | 3 23 | 24 | iex> funs = for n <- [4,3,2,1], do: &(if n == &1 do {:ok, n} else :error end) 25 | ...> ExRoseTree.Util.first_of(6, funs) 26 | nil 27 | 28 | """ 29 | @spec first_of(term(), [result_fn()]) :: term() | nil 30 | def first_of(_term, []), do: nil 31 | 32 | def first_of(term, [h | t] = _funs) when is_function(h) do 33 | case h.(term) do 34 | {:ok, result} -> result 35 | {:error, _error} -> first_of(term, t) 36 | :error -> first_of(term, t) 37 | nil -> first_of(term, t) 38 | false -> first_of(term, t) 39 | result -> result 40 | end 41 | end 42 | 43 | @doc """ 44 | Given a term, a list of potential functions to apply to the term, 45 | and a keyword list of options to apply to each function, will return 46 | with the result of the first one that succeeds when applied to the 47 | subject term. If no functions succeed, returns nil. 48 | 49 | The list of functions should each be of type `ExRoseTree.Util.result_fn()`, 50 | in other words, they should return a `ExRoseTree.Util.result()` type. 51 | 52 | ## Examples 53 | 54 | iex> fun = fn x, y, z -> 55 | ...> mult_by = Keyword.get(z, :mult_by, 1) 56 | ...> if x == y do {:ok, x * mult_by} else :error end 57 | ...> end 58 | ...> funs = for n <- [4,3,2,1], do: &fun.(n, &1, &2) 59 | ...> ExRoseTree.Util.first_of_with_opts(3, funs, [mult_by: 2]) 60 | 6 61 | 62 | """ 63 | @spec first_of_with_opts(term(), [function()], keyword()) :: term() | nil 64 | def first_of_with_opts(_term, [], _opts), do: nil 65 | 66 | def first_of_with_opts(term, [h | t] = _funs, opts) 67 | when is_function(h) and 68 | is_list(opts) do 69 | case h.(term, opts) do 70 | {:ok, result} -> result 71 | {:error, _error} -> first_of_with_opts(term, t, opts) 72 | :error -> first_of_with_opts(term, t, opts) 73 | nil -> first_of_with_opts(term, t, opts) 74 | false -> first_of_with_opts(term, t, opts) 75 | result -> result 76 | end 77 | end 78 | 79 | @doc """ 80 | Given a term, a list of potential functions to apply to the term, 81 | and a list of arguments to apply to each function, will return 82 | with the result of the first one that succeeds when applied to the 83 | subject term. If no functions succeed, returns nil. 84 | 85 | The list of functions should each be of type `ExRoseTree.Util.result_fn()`, 86 | in other words, they should return a `ExRoseTree.Util.result()` type. 87 | 88 | ## Examples 89 | 90 | iex> fun = fn x, y, add_by, sub_by -> 91 | ...> if x == y do {:ok, x + add_by - sub_by} else :error end 92 | ...> end 93 | ...> funs = for n <- [4,3,2,1], do: &fun.(n, &1, &2, &3) 94 | ...> ExRoseTree.Util.first_of_with_args(3, funs, [2, 1]) 95 | 4 96 | 97 | """ 98 | @spec first_of_with_args(term(), [function()], [term()]) :: term() | nil 99 | def first_of_with_args(_term, [], _args), do: nil 100 | 101 | def first_of_with_args(term, [h | t] = _funs, args) 102 | when is_function(h) and 103 | is_list(args) do 104 | case apply(h, [term | args]) do 105 | {:ok, result} -> result 106 | {:error, _error} -> first_of_with_args(term, t, args) 107 | :error -> first_of_with_args(term, t, args) 108 | nil -> first_of_with_args(term, t, args) 109 | false -> first_of_with_args(term, t, args) 110 | result -> result 111 | end 112 | end 113 | 114 | @doc """ 115 | A function that always returns true, regardless of what is passed to it. 116 | 117 | ## Examples 118 | 119 | iex> ExRoseTree.Util.always(5) 120 | true 121 | 122 | iex> ExRoseTree.Util.always(false) 123 | true 124 | 125 | """ 126 | @spec always(term()) :: true 127 | def always(_term), do: true 128 | 129 | @doc """ 130 | A function that always returns false, regardless of what is passed to it. 131 | 132 | ## Examples 133 | 134 | iex> ExRoseTree.Util.never(5) 135 | false 136 | 137 | iex> ExRoseTree.Util.never(true) 138 | false 139 | 140 | """ 141 | @spec never(term()) :: false 142 | def never(_term), do: false 143 | 144 | @doc """ 145 | A function that applies a predicate to a term. If the function application 146 | is true, returns the original term. If false, returns nil. 147 | 148 | ## Examples 149 | 150 | iex> ExRoseTree.Util.maybe(5, &(&1 == 5)) 151 | 5 152 | 153 | iex> ExRoseTree.Util.maybe(5, &(&1 == 1)) 154 | nil 155 | 156 | """ 157 | @spec maybe(term(), (term() -> boolean())) :: term() | nil 158 | def maybe(value, predicate) when is_function(predicate) do 159 | if predicate.(value) == true do 160 | value 161 | else 162 | nil 163 | end 164 | end 165 | 166 | @doc """ 167 | Similar to `Enum.split/2` but with specialized behavior. It is 168 | optimized to return the list of elements that come before the 169 | index in reverse order. This is ideal for the context-aware nature 170 | of Zippers. Also unlike `Enum.split/2`, if given an index that 171 | is greater than or equal to the total elements in the given list or 172 | if given a negative index, this function will _not_ perform a split, 173 | and will return two empty lists. 174 | 175 | ## Examples 176 | 177 | iex> ExRoseTree.Util.split_at([1,2,3,4,5], 2) 178 | {[2, 1], [3, 4, 5]} 179 | 180 | iex> ExRoseTree.Util.split_at([1,2,3,4,5], 10) 181 | {[], []} 182 | 183 | """ 184 | @spec split_at(list(), non_neg_integer()) :: {[term()], [term()]} 185 | def split_at([], _), do: {[], []} 186 | 187 | def split_at(elements, index) 188 | when is_list(elements) and 189 | is_integer(index) and 190 | index >= 0 do 191 | if index >= Enum.count(elements) do 192 | {[], []} 193 | else 194 | {_current_idx, prev, next} = 195 | elements 196 | |> Enum.reduce( 197 | {0, [], []}, 198 | fn entry, {current_idx, prev, next} -> 199 | if current_idx >= index do 200 | {current_idx + 1, prev, [entry | next]} 201 | else 202 | {current_idx + 1, [entry | prev], next} 203 | end 204 | end 205 | ) 206 | 207 | {prev, Enum.reverse(next)} 208 | end 209 | end 210 | 211 | def split_at(elements, index) 212 | when is_list(elements) and 213 | is_integer(index), 214 | do: {[], []} 215 | 216 | @doc """ 217 | Similar to `Enum.split/2`, `Enum.split_while/2`, and `Enum.split_with/2`, 218 | `split_when/2` instead takes a list of elements and a predicate to apply 219 | to each element. The first element that passes the predicate is where 220 | the list of elements will be split, with the target element as the head 221 | of the second list in the return value. Like with `split_at/2`, the first 222 | list of elements are returned in reverse order. 223 | 224 | ## Examples 225 | 226 | iex> ExRoseTree.Util.split_when([1,2,3,4,5], fn x -> x == 3 end) 227 | {[2, 1], [3, 4, 5]} 228 | 229 | """ 230 | @spec split_when(list(), predicate :: (term() -> boolean())) :: {[term()], [term()]} 231 | def split_when([], predicate) when is_function(predicate), do: {[], []} 232 | 233 | def split_when(elements, predicate) 234 | when is_list(elements) and is_function(predicate) do 235 | do_split_when([], elements, predicate) 236 | end 237 | 238 | @spec do_split_when(list(), list(), (term() -> boolean())) :: {[term()], [term()]} 239 | defp do_split_when(_acc, [] = _remaining, _predicate), do: {[], []} 240 | 241 | defp do_split_when(acc, [head | tail] = remaining, predicate) do 242 | if predicate.(head) == true do 243 | {acc, remaining} 244 | else 245 | do_split_when([head | acc], tail, predicate) 246 | end 247 | end 248 | end 249 | -------------------------------------------------------------------------------- /lib/ex_rose_tree/zipper/location.ex: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Zipper.Location do 2 | @moduledoc """ 3 | A `Location` in the `path` from the root of the `ExRoseTree.Zipper` to its 4 | current context. 5 | """ 6 | 7 | require ExRoseTree 8 | 9 | defstruct ~w(prev term next)a 10 | 11 | @typedoc """ 12 | A `Location` is made up of the `term` of an `ExRoseTree` with lists of `prev` and `next` siblings. 13 | 14 | * `term` is an `ExRoseTree` `term`. 15 | * `prev` is a list of `ExRoseTree`s. They are the siblings that 16 | occur prior the `term`. It is reversed such that the 17 | head of the list is the nearest previous sibling. 18 | * `next` is a list of `ExRoseTree`s. They are the siblings that 19 | occur after the `term`. 20 | """ 21 | @type t :: %__MODULE__{ 22 | prev: [ExRoseTree.t()], 23 | term: term(), 24 | next: [ExRoseTree.t()] 25 | } 26 | 27 | @doc section: :guards 28 | defguard location?(value) 29 | when is_struct(value) and 30 | value.__struct__ == __MODULE__ and 31 | is_list(value.prev) and 32 | is_list(value.next) 33 | 34 | @doc """ 35 | Builds a new `Location` given a `term()` or an `ExRoseTree` as the first 36 | argument, and optional `:prev` and `:next` keywords of lists of `ExRoseTree`s. 37 | 38 | If the first argument is an `ExRoseTree`, it will unwrap its `term` element. 39 | 40 | ## Examples 41 | 42 | iex> ExRoseTree.Zipper.Location.new(5, prev: [], next: []) 43 | %ExRoseTree.Zipper.Location{prev: [], term: 5, next: []} 44 | 45 | iex> tree = ExRoseTree.new(4) 46 | ...> ExRoseTree.Zipper.Location.new(5, prev: [tree], next: []) 47 | %ExRoseTree.Zipper.Location{ 48 | prev: [ 49 | %ExRoseTree{term: 4, children: []} 50 | ], 51 | term: 5, 52 | next: [] 53 | } 54 | 55 | """ 56 | @spec new(ExRoseTree.t() | term(), keyword()) :: t() | nil 57 | def new(item, opts \\ []) 58 | 59 | def new(item, opts) when ExRoseTree.rose_tree?(item) do 60 | new(item.term, opts) 61 | end 62 | 63 | def new(item, opts) do 64 | prev = Keyword.get(opts, :prev, []) 65 | next = Keyword.get(opts, :next, []) 66 | 67 | do_new(item, prev, next) 68 | end 69 | 70 | @doc false 71 | @spec do_new(ExRoseTree.t() | term(), [ExRoseTree.t()], [ExRoseTree.t()]) :: t() | nil 72 | defp do_new(item, prev, next) when is_list(prev) and is_list(next) do 73 | case {ExRoseTree.all_rose_trees?(prev), ExRoseTree.all_rose_trees?(next)} do 74 | {true, true} -> 75 | %__MODULE__{ 76 | prev: prev, 77 | term: item, 78 | next: next 79 | } 80 | 81 | {true, false} -> 82 | raise ArgumentError, "invalid element in prev" 83 | 84 | {false, true} -> 85 | raise ArgumentError, "invalid element in next" 86 | end 87 | end 88 | 89 | @doc """ 90 | Returns whether a list of values are all `Location`s or not. Will return 91 | `true` if passed an empty list. 92 | 93 | ## Examples 94 | 95 | iex> locs = for loc <- [5,4,3,2,1], do: ExRoseTree.Zipper.Location.new(loc) 96 | ...> ExRoseTree.Zipper.Location.all_locations?(locs) 97 | true 98 | 99 | """ 100 | @spec all_locations?([t()]) :: boolean() 101 | def all_locations?(values) when is_list(values) do 102 | Enum.all?(values, &location?(&1)) 103 | end 104 | 105 | @doc """ 106 | Applies the given function to the `Location`'s `term` field. 107 | 108 | ## Examples 109 | 110 | iex> loc = ExRoseTree.Zipper.Location.new(5, prev: [], next: []) 111 | ...> ExRoseTree.Zipper.Location.map_term(loc, &(&1*2)) 112 | %ExRoseTree.Zipper.Location{prev: [], term: 10, next: []} 113 | 114 | """ 115 | @spec map_term(t(), (term() -> term())) :: t() 116 | def map_term(%__MODULE__{term: term} = location, map_fn) when is_function(map_fn) do 117 | %{location | term: map_fn.(term)} 118 | end 119 | 120 | @doc """ 121 | Returns the index of the `Location` in relation to its siblings. 122 | 123 | ## Examples 124 | 125 | iex> trees = for t <- [5,4,3,2,1], do: ExRoseTree.new(t) 126 | ...> loc = ExRoseTree.Zipper.Location.new(6, prev: trees, next: []) 127 | ...> ExRoseTree.Zipper.Location.index_of_term(loc) 128 | 5 129 | 130 | """ 131 | @spec index_of_term(t()) :: non_neg_integer() 132 | def index_of_term(%__MODULE__{prev: prev}), 133 | do: Enum.count(prev) 134 | end 135 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.MixProject do 2 | use Mix.Project 3 | 4 | @description "A Rose Tree and Zipper in Elixir with a slew of navigation primitives." 5 | @source_url "https://github.com/metacode-io/ex-rose-tree" 6 | @version "0.1.3" 7 | 8 | def project do 9 | [ 10 | app: :ex_rose_tree, 11 | version: @version, 12 | elixir: "~> 1.14", 13 | elixirc_paths: elixirc_paths(Mix.env()), 14 | start_permanent: Mix.env() == :prod, 15 | name: "ExRoseTree", 16 | description: @description, 17 | package: package(), 18 | deps: deps(), 19 | docs: docs(), 20 | source_url: @source_url, 21 | test_coverage: [tool: ExCoveralls], 22 | preferred_cli_env: [ 23 | coveralls: :test 24 | ] 25 | ] 26 | end 27 | 28 | # Run "mix help compile.app" to learn about applications. 29 | def application do 30 | [ 31 | extra_applications: [:logger] 32 | ] 33 | end 34 | 35 | defp elixirc_paths(:test), do: ["lib", "dev", "test/support"] 36 | defp elixirc_paths(:dev), do: ["lib", "dev"] 37 | defp elixirc_paths(_env), do: ["lib"] 38 | 39 | defp docs() do 40 | [ 41 | source_url: @source_url, 42 | source_ref: "v#{@version}", 43 | language: "en", 44 | formatters: ["html"], 45 | main: "ExRoseTree", 46 | groups_for_modules: [ 47 | Tree: [ 48 | ExRoseTree 49 | ], 50 | Zipper: [ 51 | ExRoseTree.Zipper, 52 | ExRoseTree.Zipper.Location 53 | ], 54 | Util: [ 55 | ExRoseTree.Util 56 | ], 57 | "Dev Support": [ 58 | ExRoseTree.Support.Generators, 59 | ExRoseTree.Support.Trees, 60 | ExRoseTree.Support.Zippers 61 | ] 62 | ], 63 | groups_for_docs: [ 64 | Guards: &(&1[:section] == :guards), 65 | "Basic Functionality": &(&1[:section] == :basic), 66 | Term: &(&1[:section] == :term), 67 | Children: &(&1[:section] == :children), 68 | "Common Traversal": &(&1[:section] == :traversal), 69 | "Path Traversal": &(&1[:section] == :path_traversal), 70 | "Breadth-first Traversal": &(&1[:section] == :breadth_first), 71 | "Depth-first Traversal": &(&1[:section] == :depth_first), 72 | "Direct Ancestors": &(&1[:section] == :ancestors), 73 | "Direct Descendants": &(&1[:section] == :descendants), 74 | Siblings: &(&1[:section] == :siblings), 75 | "Niblings: Nieces & Nephews": &(&1[:section] == :niblings), 76 | "Piblings: Aunts & Uncles": &(&1[:section] == :piblings), 77 | "First Cousins": &(&1[:section] == :first_cousins), 78 | "Second Cousins": &(&1[:section] == :second_cousins), 79 | "Extended Cousins": &(&1[:section] == :extended_cousins), 80 | Special: &(&1[:section] == :special) 81 | ], 82 | extras: [] 83 | ] 84 | end 85 | 86 | defp package() do 87 | [ 88 | maintainers: ["Matthew Caldwell"], 89 | licenses: ["Apache-2.0"], 90 | files: 91 | ~w(CHANGELOG.md CODE_OF_CONDUCT.md CONTRIBUTING.md .formatter.exs lib dev LICENSE* mix.exs README.md), 92 | links: %{"GitHub" => @source_url} 93 | ] 94 | end 95 | 96 | # Run "mix help deps" to learn about dependencies. 97 | defp deps do 98 | [ 99 | {:credo, "~> 1.6.4", only: [:dev, :test], runtime: false}, 100 | {:dialyxir, "~> 1.0", only: [:dev], runtime: false}, 101 | {:excoveralls, "~> 0.14.5", only: :test}, 102 | {:ex_doc, "~> 0.29", only: :dev, runtime: false}, 103 | {:benchee, "~> 1.1", only: :dev} 104 | ] 105 | end 106 | end 107 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "benchee": {:hex, :benchee, "1.1.0", "f3a43817209a92a1fade36ef36b86e1052627fd8934a8b937ac9ab3a76c43062", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}], "hexpm", "7da57d545003165a012b587077f6ba90b89210fd88074ce3c60ce239eb5e6d93"}, 3 | "bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"}, 4 | "certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"}, 5 | "credo": {:hex, :credo, "1.6.7", "323f5734350fd23a456f2688b9430e7d517afb313fbd38671b8a4449798a7854", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "41e110bfb007f7eda7f897c10bf019ceab9a0b269ce79f015d54b0dcf4fc7dd3"}, 6 | "deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"}, 7 | "dialyxir": {:hex, :dialyxir, "1.2.0", "58344b3e87c2e7095304c81a9ae65cb68b613e28340690dfe1a5597fd08dec37", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "61072136427a851674cab81762be4dbeae7679f85b1272b6d25c3a839aff8463"}, 8 | "earmark_parser": {:hex, :earmark_parser, "1.4.29", "149d50dcb3a93d9f3d6f3ecf18c918fb5a2d3c001b5d3305c926cddfbd33355b", [:mix], [], "hexpm", "4902af1b3eb139016aed210888748db8070b8125c2342ce3dcae4f38dcc63503"}, 9 | "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, 10 | "ex_doc": {:hex, :ex_doc, "0.29.1", "b1c652fa5f92ee9cf15c75271168027f92039b3877094290a75abcaac82a9f77", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "b7745fa6374a36daf484e2a2012274950e084815b936b1319aeebcf7809574f6"}, 11 | "excoveralls": {:hex, :excoveralls, "0.14.6", "610e921e25b180a8538229ef547957f7e04bd3d3e9a55c7c5b7d24354abbba70", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "0eceddaa9785cfcefbf3cd37812705f9d8ad34a758e513bb975b081dce4eb11e"}, 12 | "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, 13 | "hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~> 2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"}, 14 | "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, 15 | "jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"}, 16 | "makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"}, 17 | "makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"}, 18 | "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"}, 19 | "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, 20 | "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, 21 | "nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"}, 22 | "parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"}, 23 | "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, 24 | "statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"}, 25 | "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, 26 | } 27 | -------------------------------------------------------------------------------- /test/ex_rose_tree/util_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.UtilTest do 2 | use ExUnit.Case 3 | 4 | alias ExRoseTree.Util 5 | 6 | doctest ExRoseTree.Util 7 | 8 | setup_all do 9 | # pass functions 10 | ok_tuple_fn = fn _ -> {:ok, :success_1} end 11 | result_fn = fn _ -> :success_2 end 12 | 13 | pass_fns = [ok_tuple_fn, result_fn] 14 | 15 | # fail functions 16 | error_tuple_fn = fn _ -> {:error, :failure} end 17 | error_fn = fn _ -> :error end 18 | nil_fn = fn _ -> nil end 19 | false_fn = fn _ -> false end 20 | 21 | fail_fns = [error_tuple_fn, error_fn, nil_fn, false_fn] 22 | 23 | all_fns = pass_fns ++ fail_fns 24 | 25 | # pass functions with opts 26 | ok_tuple_opts_fn = fn _, opts -> {:ok, {:success_1, opts}} end 27 | result_opts_fn = fn _, opts -> {:success_2, opts} end 28 | 29 | pass_opts_fns = [ok_tuple_opts_fn, result_opts_fn] 30 | 31 | # fail functions with opts 32 | error_tuple_opts_fn = fn _, _ -> {:error, :failure} end 33 | error_opts_fn = fn _, _ -> :error end 34 | nil_opts_fn = fn _, _ -> nil end 35 | false_opts_fn = fn _, _ -> false end 36 | 37 | fail_opts_fns = [error_tuple_opts_fn, error_opts_fn, nil_opts_fn, false_opts_fn] 38 | 39 | all_opts_fns = pass_opts_fns ++ fail_opts_fns 40 | 41 | # pass functions with args 42 | ok_tuple_args_fn = fn _, arg1, arg2 -> {:ok, {:success_1, arg1, arg2}} end 43 | result_args_fn = fn _, arg1, arg2 -> {:success_2, arg1, arg2} end 44 | 45 | pass_args_fns = [ok_tuple_args_fn, result_args_fn] 46 | 47 | # fail functions with args 48 | error_tuple_args_fn = fn _, _, _ -> {:error, :failure} end 49 | error_args_fn = fn _, _, _ -> :error end 50 | nil_args_fn = fn _, _, _ -> nil end 51 | false_args_fn = fn _, _, _ -> false end 52 | 53 | fail_args_fns = [error_tuple_args_fn, error_args_fn, nil_args_fn, false_args_fn] 54 | 55 | all_args_fns = pass_args_fns ++ fail_args_fns 56 | 57 | %{ 58 | # regular fns 59 | pass_fns: pass_fns, 60 | fail_fns: fail_fns, 61 | all_fns: all_fns, 62 | ok_tuple_fn: ok_tuple_fn, 63 | result_fn: result_fn, 64 | error_tuple_fn: error_tuple_fn, 65 | error_fn: error_fn, 66 | nil_fn: nil_fn, 67 | false_fn: false_fn, 68 | # fns with keyword opts 69 | pass_opts_fns: pass_opts_fns, 70 | fail_opts_fns: fail_opts_fns, 71 | all_opts_fns: all_opts_fns, 72 | ok_tuple_opts_fn: ok_tuple_opts_fn, 73 | result_opts_fn: result_opts_fn, 74 | error_tuple_opts_fn: error_tuple_opts_fn, 75 | error_opts_fn: error_opts_fn, 76 | nil_opts_fn: nil_opts_fn, 77 | false_opts_fn: false_opts_fn, 78 | # fns with args list 79 | pass_args_fns: pass_args_fns, 80 | fail_args_fns: fail_args_fns, 81 | all_args_fns: all_args_fns, 82 | ok_tuple_args_fn: ok_tuple_args_fn, 83 | result_args_fn: result_args_fn, 84 | error_tuple_args_fn: error_tuple_args_fn, 85 | error_args_fn: error_args_fn, 86 | nil_args_fn: nil_args_fn, 87 | false_args_fn: false_args_fn 88 | } 89 | end 90 | 91 | describe "first_of/2" do 92 | test "should return nil if given an empty list for second parameter" do 93 | assert nil == Util.first_of(:anything, []) 94 | end 95 | 96 | test "should return nil if single function provided and returns {:error, error}", %{ 97 | error_tuple_fn: function 98 | } do 99 | assert nil == Util.first_of(:anything, [function]) 100 | end 101 | 102 | test "should return nil if single function provided and returns :error", %{error_fn: function} do 103 | assert nil == Util.first_of(:anything, [function]) 104 | end 105 | 106 | test "should return nil if single function provided and returns nil", %{nil_fn: function} do 107 | assert nil == Util.first_of(:anything, [function]) 108 | end 109 | 110 | test "should return nil if single function provided and returns false", %{false_fn: function} do 111 | assert nil == Util.first_of(:anything, [function]) 112 | end 113 | 114 | test "should return nil if all provided functions fail", %{fail_fns: functions} do 115 | assert nil == Util.first_of(:anything, functions) 116 | end 117 | 118 | test "should return successful result if single function provided that returns {:ok, value}", 119 | %{ok_tuple_fn: function} do 120 | assert :success_1 == Util.first_of(:anything, [function]) 121 | end 122 | 123 | test "should return successful result if single function provided that returns a truthy result", 124 | %{result_fn: function} do 125 | assert :success_2 == Util.first_of(:anything, [function]) 126 | end 127 | 128 | test "should return a successful result for a randomly mixed list of functions as long as at least one passes", 129 | %{all_fns: functions} do 130 | result = Util.first_of(:anything, Enum.shuffle(functions)) 131 | assert result in [:success_1, :success_2] 132 | end 133 | 134 | test "should return the first successful result", %{ 135 | ok_tuple_fn: fn_1, 136 | result_fn: fn_2, 137 | error_fn: fn_3 138 | } do 139 | functions_1 = [fn_3, fn_1, fn_2] 140 | functions_2 = [fn_3, fn_2, fn_1] 141 | 142 | assert :success_1 == Util.first_of(:anything, functions_1) 143 | assert :success_2 == Util.first_of(:anything, functions_2) 144 | end 145 | end 146 | 147 | @options [option_1: true, option_2: 5] 148 | 149 | describe "first_of_with_opts/2" do 150 | test "should return nil if given an empty list for second parameter" do 151 | assert nil == Util.first_of_with_opts(:anything, [], @options) 152 | end 153 | 154 | test "should return nil if single function provided and returns {:error, error}", %{ 155 | error_tuple_opts_fn: function 156 | } do 157 | assert nil == Util.first_of_with_opts(:anything, [function], @options) 158 | end 159 | 160 | test "should return nil if single function provided and returns :error", %{ 161 | error_opts_fn: function 162 | } do 163 | assert nil == Util.first_of_with_opts(:anything, [function], @options) 164 | end 165 | 166 | test "should return nil if single function provided and returns nil", %{nil_opts_fn: function} do 167 | assert nil == Util.first_of_with_opts(:anything, [function], @options) 168 | end 169 | 170 | test "should return nil if single function provided and returns false", %{ 171 | false_opts_fn: function 172 | } do 173 | assert nil == Util.first_of_with_opts(:anything, [function], @options) 174 | end 175 | 176 | test "should return nil if all provided functions fail", %{fail_opts_fns: functions} do 177 | assert nil == Util.first_of_with_opts(:anything, functions, @options) 178 | end 179 | 180 | test "should return successful result if single function provided that returns {:ok, value}", 181 | %{ok_tuple_opts_fn: function} do 182 | assert {:success_1, @options} == Util.first_of_with_opts(:anything, [function], @options) 183 | end 184 | 185 | test "should return successful result if single function provided that returns a truthy result", 186 | %{result_opts_fn: function} do 187 | assert {:success_2, @options} == Util.first_of_with_opts(:anything, [function], @options) 188 | end 189 | 190 | test "should return a successful result for a randomly mixed list of functions as long as at least one passes", 191 | %{all_opts_fns: functions} do 192 | assert {result, @options} = 193 | Util.first_of_with_opts(:anything, Enum.shuffle(functions), @options) 194 | 195 | assert result in [:success_1, :success_2] 196 | end 197 | 198 | test "should return the first successful result", %{ 199 | ok_tuple_opts_fn: fn_1, 200 | result_opts_fn: fn_2, 201 | error_opts_fn: fn_3 202 | } do 203 | functions_1 = [fn_3, fn_1, fn_2] 204 | functions_2 = [fn_3, fn_2, fn_1] 205 | 206 | assert {:success_1, @options} == Util.first_of_with_opts(:anything, functions_1, @options) 207 | assert {:success_2, @options} == Util.first_of_with_opts(:anything, functions_2, @options) 208 | end 209 | end 210 | 211 | @arg1 true 212 | @arg2 5 213 | @args [@arg1, @arg2] 214 | 215 | describe "first_of_with_args/2" do 216 | test "should return nil if given an empty list for second parameter" do 217 | assert nil == Util.first_of_with_args(:anything, [], @args) 218 | end 219 | 220 | test "should return nil if single function provided and returns {:error, error}", %{ 221 | error_tuple_args_fn: function 222 | } do 223 | assert nil == Util.first_of_with_args(:anything, [function], @args) 224 | end 225 | 226 | test "should return nil if single function provided and returns :error", %{ 227 | error_args_fn: function 228 | } do 229 | assert nil == Util.first_of_with_args(:anything, [function], @args) 230 | end 231 | 232 | test "should return nil if single function provided and returns nil", %{nil_args_fn: function} do 233 | assert nil == Util.first_of_with_args(:anything, [function], @args) 234 | end 235 | 236 | test "should return nil if single function provided and returns false", %{ 237 | false_args_fn: function 238 | } do 239 | assert nil == Util.first_of_with_args(:anything, [function], @args) 240 | end 241 | 242 | test "should return nil if all provided functions fail", %{fail_args_fns: functions} do 243 | assert nil == Util.first_of_with_args(:anything, functions, @args) 244 | end 245 | 246 | test "should return successful result if single function provided that returns {:ok, value}", 247 | %{ok_tuple_args_fn: function} do 248 | assert {:success_1, @arg1, @arg2} == Util.first_of_with_args(:anything, [function], @args) 249 | end 250 | 251 | test "should return successful result if single function provided that returns a truthy result", 252 | %{result_args_fn: function} do 253 | assert {:success_2, @arg1, @arg2} == Util.first_of_with_args(:anything, [function], @args) 254 | end 255 | 256 | test "should return a successful result for a randomly mixed list of functions as long as at least one passes", 257 | %{all_args_fns: functions} do 258 | assert {result, @arg1, @arg2} = 259 | Util.first_of_with_args(:anything, Enum.shuffle(functions), @args) 260 | 261 | assert result in [:success_1, :success_2] 262 | end 263 | 264 | test "should return the first successful result", %{ 265 | ok_tuple_args_fn: fn_1, 266 | result_args_fn: fn_2, 267 | error_args_fn: fn_3 268 | } do 269 | functions_1 = [fn_3, fn_1, fn_2] 270 | functions_2 = [fn_3, fn_2, fn_1] 271 | 272 | assert {:success_1, @arg1, @arg2} == Util.first_of_with_args(:anything, functions_1, @args) 273 | assert {:success_2, @arg1, @arg2} == Util.first_of_with_args(:anything, functions_2, @args) 274 | end 275 | end 276 | 277 | describe "split_at/2" do 278 | test "should return two empty lists if given an empty list" do 279 | random_idx = Enum.random(1..100) 280 | assert {[], []} = Util.split_at([], random_idx) 281 | end 282 | 283 | test "should return two empty lists if given index greater than list count" do 284 | assert {[], []} = Util.split_at([1, 2, 3, 4, 5], 10) 285 | end 286 | 287 | test "should return two empty lists if given index equal to the list count" do 288 | assert {[], []} = Util.split_at([1, 2, 3, 4, 5], 5) 289 | end 290 | 291 | test "should return two empty lists if given a negative index" do 292 | random_idx = Enum.random(-1..-100) 293 | assert {[], []} = Util.split_at([1, 2, 3, 4, 5], random_idx) 294 | end 295 | 296 | test "should return empty list and in-order list when index = 0" do 297 | list = [1, 2, 3, 4, 5] 298 | assert {[], ^list} = Util.split_at(list, 0) 299 | end 300 | 301 | test "should return reverse partition and in-order partition when index is in-bounds and not at border" do 302 | list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 303 | expected_1 = [5, 4, 3, 2, 1] 304 | expected_2 = [6, 7, 8, 9, 10] 305 | assert {^expected_1, ^expected_2} = Util.split_at(list, 5) 306 | end 307 | end 308 | 309 | describe "split_when/2" do 310 | test "should return two empty lists if given an empty list" do 311 | random_idx = Enum.random(1..100) 312 | predicate = &(&1 == random_idx) 313 | assert {[], []} = Util.split_when([], predicate) 314 | end 315 | 316 | test "should return two empty lists if predicate never matches" do 317 | assert {[], []} = Util.split_when([1, 2, 3, 4, 5], &Util.never/1) 318 | end 319 | 320 | test "should return two empty lists if given a bad predicate" do 321 | assert {[], []} = Util.split_when([1, 2, 3, 4, 5], fn _ -> :bad end) 322 | end 323 | 324 | test "should return empty list and in-order list when predicate matches on first element" do 325 | list = [1, 2, 3, 4, 5] 326 | assert {[], ^list} = Util.split_when(list, &(&1 == 1)) 327 | end 328 | 329 | test "should return reverse partition and in-order partition when index is in-bounds and not at border" do 330 | list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 331 | expected_1 = [5, 4, 3, 2, 1] 332 | expected_2 = [6, 7, 8, 9, 10] 333 | assert {^expected_1, ^expected_2} = Util.split_when(list, &(&1 == 6)) 334 | end 335 | end 336 | end 337 | -------------------------------------------------------------------------------- /test/ex_rose_tree/zipper/direct_ancestor_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Zipper.DirectAncestorTest do 2 | use ExUnit.Case, async: true 3 | use ZipperCase 4 | 5 | describe "parent/1" do 6 | test "should return nil for empty Zipper", %{empty_z: z} do 7 | assert Zipper.parent(z) == nil 8 | end 9 | 10 | test "should return nil for Zipper with no parent", %{simple_z: z_1, z_with_siblings: z_2} do 11 | for z <- [z_1, z_2] do 12 | assert Zipper.parent(z) == nil 13 | end 14 | end 15 | 16 | test "should move focus to parent if one is found", %{z_with_parent: z} do 17 | assert %Zipper{focus: focus} = Zipper.parent(z) 18 | assert focus.term == 10 19 | end 20 | end 21 | 22 | describe "grandparent/1" do 23 | test "should return nil for empty Zipper", %{empty_z: z} do 24 | assert Zipper.grandparent(z) == nil 25 | end 26 | 27 | test "should return nil for Zipper with no parent", %{simple_z: z} do 28 | assert Zipper.grandparent(z) == nil 29 | end 30 | 31 | test "should return nil for Zipper with no grandparent", %{z_with_parent: z} do 32 | assert Zipper.grandparent(z) == nil 33 | end 34 | 35 | test "should move focus to grandparent if one is found", 36 | %{z_with_grandparent: z} do 37 | %Zipper{focus: focus} = Zipper.grandparent(z) 38 | 39 | assert focus.term == 5 40 | end 41 | end 42 | 43 | describe "great_grandparent/1" do 44 | test "should return nil for empty Zipper", %{empty_z: z} do 45 | assert Zipper.great_grandparent(z) == nil 46 | end 47 | 48 | test "should return nil for Zipper with no parent", %{simple_z: z} do 49 | assert Zipper.great_grandparent(z) == nil 50 | end 51 | 52 | test "should return nil for Zipper with no grandparent", %{z_with_parent: z} do 53 | assert Zipper.great_grandparent(z) == nil 54 | end 55 | 56 | test "should return nil for Zipper with no great grandparent", %{z_with_grandparent: z} do 57 | assert Zipper.great_grandparent(z) == nil 58 | end 59 | 60 | test "should move focus to great grandparent if one is found", 61 | %{z_with_great_grandparent: z} do 62 | %Zipper{focus: focus} = Zipper.great_grandparent(z) 63 | 64 | assert focus.term == 1 65 | end 66 | end 67 | end 68 | -------------------------------------------------------------------------------- /test/ex_rose_tree/zipper/direct_descendant_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Zipper.DirectDescendantTest do 2 | use ExUnit.Case, async: true 3 | use ZipperCase 4 | 5 | describe "first_child/2" do 6 | test "should return nil when given a Zipper with an empty focus", %{empty_z: z} do 7 | assert Zipper.first_child(z) == nil 8 | end 9 | 10 | test "should return nil when given a Zipper with a leaf focus", %{leaf_z: z} do 11 | assert Zipper.first_child(z) == nil 12 | end 13 | 14 | test "should return nil when given a predicate that does not match any children of the Zipper", 15 | %{simple_z: z} do 16 | predicate = &(&1.term == :not_found) 17 | 18 | assert Zipper.first_child(z, predicate) == nil 19 | end 20 | end 21 | 22 | describe "last_child/2" do 23 | test "should return nil when given a Zipper with an empty focus", %{empty_z: z} do 24 | assert Zipper.last_child(z) == nil 25 | end 26 | 27 | test "should return nil when given a Zipper with a leaf focus", %{leaf_z: z} do 28 | assert Zipper.last_child(z) == nil 29 | end 30 | 31 | test "should return nil when given a predicate that does not match any children of the Zipper", 32 | %{simple_z: z} do 33 | predicate = &(&1.term == :not_found) 34 | 35 | assert Zipper.last_child(z, predicate) == nil 36 | end 37 | end 38 | 39 | describe "child_at/1" do 40 | test "should return nil when given a Zipper with an empty focus", %{empty_z: z} do 41 | for _ <- 0..5 do 42 | idx = Enum.random(0..10) 43 | assert Zipper.child_at(z, idx) == nil 44 | end 45 | end 46 | 47 | test "should return nil when given a Zipper with a leaf focus", %{leaf_z: z} do 48 | for _ <- 0..5 do 49 | idx = Enum.random(0..10) 50 | assert Zipper.child_at(z, idx) == nil 51 | end 52 | end 53 | 54 | test "should return nil when given a index that is out of bounds for the children of the Zipper", 55 | %{simple_z: z} do 56 | num_children = Enum.count(z.focus.children) 57 | 58 | for _ <- 0..5 do 59 | idx = Enum.random(num_children..10) 60 | assert Zipper.child_at(z, idx) == nil 61 | end 62 | end 63 | end 64 | 65 | describe "first_grandchild/2" do 66 | test "should return the first grandchild that is found for the Zipper", %{ 67 | z_with_grandchildren: z_1, 68 | z_with_grandchildren_2: z_2 69 | } do 70 | for z <- [z_1, z_2] do 71 | actual = Zipper.first_grandchild(z) 72 | assert 4 == actual.focus.term 73 | end 74 | end 75 | 76 | test "should return the first grandchild that is found that matches the predicate for the Zipper", 77 | %{z_with_grandchildren: z_1, z_with_grandchildren_2: z_2} do 78 | predicate = &(&1.term > 7) 79 | 80 | for z <- [z_1, z_2] do 81 | actual = Zipper.first_grandchild(z, predicate) 82 | assert 8 == actual.focus.term 83 | end 84 | end 85 | 86 | test "should return nil if Zipper has children but no grandchildren", %{simple_z: z} do 87 | assert Zipper.first_grandchild(z) == nil 88 | end 89 | 90 | test "should return nil if Zipper has no children", %{leaf_z: z} do 91 | assert Zipper.first_grandchild(z) == nil 92 | end 93 | 94 | test "should return nil if no grandchild is found that matches the predicate for the Zipper", 95 | %{z_with_grandchildren: z_1, z_with_grandchildren_2: z_2} do 96 | predicate = &(&1.term == 20) 97 | 98 | for z <- [z_1, z_2] do 99 | assert Zipper.first_grandchild(z, predicate) == nil 100 | end 101 | end 102 | end 103 | 104 | describe "last_grandchild/2" do 105 | test "should return the last grandchild that is found for the Zipper", %{ 106 | z_with_grandchildren: z_1, 107 | z_with_grandchildren_2: z_2 108 | } do 109 | for z <- [z_1, z_2] do 110 | actual = Zipper.last_grandchild(z) 111 | assert 12 == actual.focus.term 112 | end 113 | end 114 | 115 | test "should return the last grandchild that is found that matches the predicate for the Zipper", 116 | %{z_with_grandchildren: z_1, z_with_grandchildren_2: z_2} do 117 | predicate = &(&1.term < 9) 118 | 119 | for z <- [z_1, z_2] do 120 | actual = Zipper.last_grandchild(z, predicate) 121 | assert 8 == actual.focus.term 122 | end 123 | end 124 | 125 | test "should return nil if Zipper has children but no grandchildren", %{simple_z: z} do 126 | assert Zipper.last_grandchild(z) == nil 127 | end 128 | 129 | test "should return nil if Zipper has no children", %{leaf_z: z} do 130 | assert Zipper.last_grandchild(z) == nil 131 | end 132 | 133 | test "should return nil if no grandchild is found that matches the predicate for the Zipper", 134 | %{z_with_grandchildren: z_1, z_with_grandchildren_2: z_2} do 135 | predicate = &(&1.term == 20) 136 | 137 | for z <- [z_1, z_2] do 138 | assert Zipper.last_grandchild(z, predicate) == nil 139 | end 140 | end 141 | end 142 | 143 | describe "first_great_grandchild/2" do 144 | test "should return the first great grandchild that is found for the Zipper", %{ 145 | z_with_great_grandchildren: z_1, 146 | z_with_great_grandchildren_2: z_2 147 | } do 148 | for z <- [z_1, z_2] do 149 | actual = Zipper.first_great_grandchild(z) 150 | assert 13 == actual.focus.term 151 | end 152 | end 153 | 154 | test "should return the first grandchild that is found that matches the predicate for the Zipper", 155 | %{z_with_great_grandchildren: z_1, z_with_great_grandchildren_2: z_2} do 156 | predicate = &(&1.term > 16) 157 | 158 | for z <- [z_1, z_2] do 159 | actual = Zipper.first_great_grandchild(z, predicate) 160 | assert 17 == actual.focus.term 161 | end 162 | end 163 | 164 | test "should return nil if Zipper has children but no grandchildren", %{simple_z: z} do 165 | assert Zipper.first_great_grandchild(z) == nil 166 | end 167 | 168 | test "should return nil if Zipper has no children", %{leaf_z: z} do 169 | assert Zipper.first_great_grandchild(z) == nil 170 | end 171 | 172 | test "should return nil if no grandchild is found that matches the predicate for the Zipper", 173 | %{z_with_great_grandchildren: z_1, z_with_great_grandchildren_2: z_2} do 174 | predicate = &(&1.term == 30) 175 | 176 | for z <- [z_1, z_2] do 177 | assert Zipper.first_great_grandchild(z, predicate) == nil 178 | end 179 | end 180 | end 181 | 182 | describe "last_great_grandchild/2" do 183 | test "should return the last great grandchild that is found for the Zipper", %{ 184 | z_with_great_grandchildren: z_1, 185 | z_with_great_grandchildren_2: z_2 186 | } do 187 | for z <- [z_1, z_2] do 188 | actual = Zipper.last_great_grandchild(z) 189 | assert 21 == actual.focus.term 190 | end 191 | end 192 | 193 | test "should return the last grandchild that is found that matches the predicate for the Zipper", 194 | %{z_with_great_grandchildren: z_1, z_with_great_grandchildren_2: z_2} do 195 | predicate = &(&1.term < 18) 196 | 197 | for z <- [z_1, z_2] do 198 | actual = Zipper.last_great_grandchild(z, predicate) 199 | assert 17 == actual.focus.term 200 | end 201 | end 202 | 203 | test "should return nil if Zipper has children but no grandchildren", %{simple_z: z} do 204 | assert Zipper.last_great_grandchild(z) == nil 205 | end 206 | 207 | test "should return nil if Zipper has no children", %{leaf_z: z} do 208 | assert Zipper.last_great_grandchild(z) == nil 209 | end 210 | 211 | test "should return nil if no grandchild is found that matches the predicate for the Zipper", 212 | %{z_with_great_grandchildren: z_1, z_with_great_grandchildren_2: z_2} do 213 | predicate = &(&1.term == 30) 214 | 215 | for z <- [z_1, z_2] do 216 | assert Zipper.last_great_grandchild(z, predicate) == nil 217 | end 218 | end 219 | end 220 | 221 | describe "rightmost_descendant/2" do 222 | test "should return nil when given a zipper with no children", 223 | %{empty_z: z_0, leaf_z: z_1, z_with_siblings: z_2} do 224 | for z <- [z_0, z_1, z_2] do 225 | assert Zipper.rightmost_descendant(z) == nil 226 | end 227 | end 228 | 229 | test "should return the rightmost descendant of the Zipper", %{z_with_great_grandchildren: z} do 230 | assert %Zipper{focus: actual} = Zipper.rightmost_descendant(z) 231 | assert actual.term == 12 232 | end 233 | 234 | test "should return the rightmost descendant of the Zipper if predicate matches", %{ 235 | z_with_great_grandchildren: z 236 | } do 237 | predicate = &(&1.focus.term == 3) 238 | 239 | assert %Zipper{focus: actual} = Zipper.rightmost_descendant(z, predicate) 240 | assert actual.term == 3 241 | end 242 | 243 | test "should return rightmost descendant of the Zipper if predicate doesn't find a match", %{ 244 | z_with_great_grandchildren: z 245 | } do 246 | predicate = &(&1.focus.term == 30) 247 | 248 | assert %Zipper{focus: actual} = Zipper.rightmost_descendant(z, predicate) 249 | assert actual.term == 12 250 | end 251 | end 252 | 253 | describe "leftmost_descendant/2" do 254 | test "should return nil when given a zipper with no children", 255 | %{empty_z: z_0, leaf_z: z_1, z_with_siblings: z_2} do 256 | for z <- [z_0, z_1, z_2] do 257 | assert Zipper.leftmost_descendant(z) == nil 258 | end 259 | end 260 | 261 | test "should return the leftmost descendant of the Zipper", %{z_with_great_grandchildren: z} do 262 | assert %Zipper{focus: actual} = Zipper.leftmost_descendant(z) 263 | assert actual.term == 4 264 | end 265 | 266 | test "should return the rightmost descendant of the Zipper if predicate matches", %{ 267 | z_with_great_grandchildren: z 268 | } do 269 | predicate = &(&1.focus.term == 1) 270 | 271 | assert %Zipper{focus: actual} = Zipper.leftmost_descendant(z, predicate) 272 | assert actual.term == 1 273 | end 274 | 275 | test "should return rightmost descendant of the Zipper if predicate doesn't find a match", %{ 276 | z_with_great_grandchildren: z 277 | } do 278 | predicate = &(&1.focus.term == 40) 279 | 280 | assert %Zipper{focus: actual} = Zipper.leftmost_descendant(z, predicate) 281 | assert actual.term == 4 282 | end 283 | end 284 | end 285 | -------------------------------------------------------------------------------- /test/ex_rose_tree/zipper/extended_cousin_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Zipper.ExtendedCousinTest do 2 | use ExUnit.Case, async: true 3 | use ZipperCase 4 | 5 | describe "first_extended_cousin/2" do 6 | test "should return nil if no parent found", %{simple_z: z} do 7 | assert Zipper.first_extended_cousin(z) == nil 8 | end 9 | 10 | test "should return nil if no grandparent found", %{z_with_parent: z} do 11 | assert Zipper.first_extended_cousin(z) == nil 12 | end 13 | 14 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 15 | assert Zipper.first_extended_cousin(z) == nil 16 | end 17 | 18 | test "should return nil if no previous grandpibling has children", 19 | %{z_with_grandpiblings: z} do 20 | assert Zipper.first_extended_cousin(z) == nil 21 | end 22 | 23 | test "should return the same value as Zipper.first_first_cousin/2 when no further extended cousins exist", 24 | %{z_with_1st_cousins: z} do 25 | assert %Zipper{focus: expected} = Zipper.first_first_cousin(z) 26 | 27 | assert %Zipper{focus: actual} = Zipper.first_extended_cousin(z) 28 | 29 | assert actual.term == expected.term 30 | assert actual.term == 19 31 | end 32 | 33 | test "should return the same value as Zipper.first_second_cousin/2 when no further extended cousins exist", 34 | %{z_with_2nd_cousins: z} do 35 | assert %Zipper{focus: expected} = Zipper.first_second_cousin(z) 36 | 37 | assert %Zipper{focus: actual} = Zipper.first_extended_cousin(z) 38 | 39 | assert actual.term == expected.term 40 | assert actual.term == 50 41 | end 42 | 43 | test "should return nil if no extended cousin found matching predicate", 44 | %{ 45 | z_with_1st_cousins: z_1, 46 | z_with_2nd_cousins: z_2, 47 | z_with_extended_cousins: z_3 48 | } do 49 | predicate = &(&1.term == :not_found) 50 | 51 | for z <- [z_1, z_2, z_3] do 52 | assert Zipper.first_extended_cousin(z, predicate) == nil 53 | end 54 | end 55 | 56 | test "should return the first extended cousin found", 57 | %{z_with_extended_cousins: z} do 58 | assert %Zipper{focus: actual} = Zipper.first_extended_cousin(z) 59 | assert 103 == actual.term 60 | end 61 | 62 | test "should return the first extended cousin found matching the predicate", 63 | %{z_with_extended_cousins: z} do 64 | predicate = &(&1.term == 102) 65 | 66 | assert %Zipper{focus: actual} = Zipper.first_extended_cousin(z, predicate) 67 | assert 102 == actual.term 68 | end 69 | 70 | test "should return the first extended cousin found in scenario 2", 71 | %{z_with_extended_cousins_2: z} do 72 | assert %Zipper{focus: actual} = Zipper.first_extended_cousin(z) 73 | assert -29 == actual.term 74 | end 75 | 76 | test "should return the first extended cousin found matching the predicate in scenario 2", 77 | %{z_with_extended_cousins_2: z} do 78 | predicate = &(&1.term == -31) 79 | 80 | assert %Zipper{focus: actual} = Zipper.first_extended_cousin(z, predicate) 81 | assert -31 == actual.term 82 | end 83 | end 84 | 85 | describe "last_extended_cousin/2" do 86 | test "should return nil if no parent found", %{simple_z: z} do 87 | assert Zipper.last_extended_cousin(z) == nil 88 | end 89 | 90 | test "should return nil if no grandparent found", %{z_with_parent: z} do 91 | assert Zipper.last_extended_cousin(z) == nil 92 | end 93 | 94 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 95 | assert Zipper.last_extended_cousin(z) == nil 96 | end 97 | 98 | test "should return nil if no previous grandpibling has children", 99 | %{z_with_grandpiblings: z} do 100 | assert Zipper.last_extended_cousin(z) == nil 101 | end 102 | 103 | test "should return the same value as Zipper.last_first_cousin/2 when no further extended cousins exist", 104 | %{z_with_1st_cousins: z} do 105 | assert %Zipper{focus: expected} = Zipper.last_first_cousin(z) 106 | 107 | assert %Zipper{focus: actual} = Zipper.last_extended_cousin(z) 108 | 109 | assert actual.term == expected.term 110 | assert actual.term == 30 111 | end 112 | 113 | test "should return the same value as Zipper.last_second_cousin/2 when no further extended cousins exist", 114 | %{z_with_2nd_cousins: z} do 115 | assert %Zipper{focus: expected} = Zipper.last_second_cousin(z) 116 | 117 | assert %Zipper{focus: actual} = Zipper.last_extended_cousin(z) 118 | 119 | assert actual.term == expected.term 120 | assert actual.term == 58 121 | end 122 | 123 | test "should return nil if no extended cousin found matching predicate", 124 | %{ 125 | z_with_1st_cousins: z_1, 126 | z_with_2nd_cousins: z_2, 127 | z_with_extended_cousins: z_3 128 | } do 129 | predicate = &(&1.term == :not_found) 130 | 131 | for z <- [z_1, z_2, z_3] do 132 | assert Zipper.last_extended_cousin(z, predicate) == nil 133 | end 134 | end 135 | 136 | test "should return the last extended cousin found", 137 | %{z_with_extended_cousins: z} do 138 | assert %Zipper{focus: actual} = Zipper.last_extended_cousin(z) 139 | assert 108 == actual.term 140 | end 141 | 142 | test "should return the last extended cousin found matching the predicate", 143 | %{z_with_extended_cousins: z} do 144 | predicate = &(&1.term == 106) 145 | 146 | assert %Zipper{focus: actual} = Zipper.last_extended_cousin(z, predicate) 147 | assert 106 == actual.term 148 | end 149 | 150 | test "should return the last extended cousin found in scenario 2", 151 | %{z_with_extended_cousins_2: z} do 152 | assert %Zipper{focus: actual} = Zipper.last_extended_cousin(z) 153 | assert 31 == actual.term 154 | end 155 | 156 | test "should return the last extended cousin found matching the predicate in scenario 2", 157 | %{z_with_extended_cousins_2: z} do 158 | predicate = &(&1.term == 29) 159 | 160 | assert %Zipper{focus: actual} = Zipper.last_extended_cousin(z, predicate) 161 | assert 29 == actual.term 162 | end 163 | end 164 | 165 | describe "previous_extended_cousin/2" do 166 | test "should return nil if no parent found", %{simple_z: z} do 167 | assert Zipper.previous_extended_cousin(z) == nil 168 | end 169 | 170 | test "should return nil if no grandparent found", %{z_with_parent: z} do 171 | assert Zipper.previous_extended_cousin(z) == nil 172 | end 173 | 174 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 175 | assert Zipper.previous_extended_cousin(z) == nil 176 | end 177 | 178 | test "should return nil if no previous grandpibling has children", 179 | %{z_with_grandpiblings: z} do 180 | assert Zipper.previous_extended_cousin(z) == nil 181 | end 182 | 183 | test "should return nil if no extended cousin found matching predicate", 184 | %{ 185 | z_with_1st_cousins: z_1, 186 | z_with_2nd_cousins: z_2, 187 | z_with_extended_cousins: z_3 188 | } do 189 | predicate = &(&1.term == :not_found) 190 | 191 | for z <- [z_1, z_2, z_3] do 192 | assert Zipper.previous_extended_cousin(z, predicate) == nil 193 | end 194 | end 195 | 196 | test "should return the same value as Zipper.previous_first_cousin/2 when no further extended cousins exist", 197 | %{z_with_1st_cousins: z} do 198 | assert %Zipper{focus: expected} = Zipper.previous_first_cousin(z) 199 | 200 | assert %Zipper{focus: actual} = Zipper.previous_extended_cousin(z) 201 | 202 | assert actual.term == expected.term 203 | assert actual.term == 24 204 | end 205 | 206 | test "should return the same value as Zipper.previous_second_cousin/2 when no further extended cousins exist", 207 | %{z_with_2nd_cousins: z} do 208 | assert %Zipper{focus: expected} = Zipper.previous_second_cousin(z) 209 | 210 | assert %Zipper{focus: actual} = Zipper.previous_extended_cousin(z) 211 | 212 | assert actual.term == expected.term 213 | assert actual.term == 49 214 | end 215 | 216 | test "should return the next extended cousin found", 217 | %{z_with_extended_cousins: z} do 218 | assert %Zipper{focus: actual} = Zipper.previous_extended_cousin(z) 219 | assert 102 == actual.term 220 | end 221 | 222 | test "should return the next extended cousin found matching the predicate", 223 | %{z_with_extended_cousins: z} do 224 | predicate = &(&1.term == 103) 225 | 226 | actual = Zipper.previous_extended_cousin(z, predicate) 227 | assert 103 == actual.focus.term 228 | end 229 | 230 | test "should return the next extended cousin found in scenario 2", 231 | %{z_with_extended_cousins_2: z} do 232 | actual = Zipper.previous_extended_cousin(z) 233 | assert -31 == actual.focus.term 234 | end 235 | 236 | test "should return the next extended cousin found matching the predicate in scenario 2", 237 | %{z_with_extended_cousins_2: z} do 238 | predicate = &(&1.term == -29) 239 | 240 | actual = Zipper.previous_extended_cousin(z, predicate) 241 | assert -29 == actual.focus.term 242 | end 243 | end 244 | 245 | describe "next_extended_cousin/2" do 246 | test "should return nil if no parent found", %{simple_z: z} do 247 | assert Zipper.next_extended_cousin(z) == nil 248 | end 249 | 250 | test "should return nil if no grandparent found", %{z_with_parent: z} do 251 | assert Zipper.next_extended_cousin(z) == nil 252 | end 253 | 254 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 255 | assert Zipper.next_extended_cousin(z) == nil 256 | end 257 | 258 | test "should return nil if no previous grandpibling has children", 259 | %{z_with_grandpiblings: z} do 260 | assert Zipper.next_extended_cousin(z) == nil 261 | end 262 | 263 | test "should return nil if no extended cousin found matching predicate", 264 | %{ 265 | z_with_1st_cousins: z_1, 266 | z_with_2nd_cousins: z_2, 267 | z_with_extended_cousins: z_3, 268 | z_with_extended_cousins_2: z_4 269 | } do 270 | predicate = &(&1.term == :not_found) 271 | 272 | for z <- [z_1, z_2, z_3, z_4] do 273 | assert Zipper.next_extended_cousin(z, predicate) == nil 274 | end 275 | end 276 | 277 | test "should return the same value as Zipper.next_first_cousin/2 when no further extended cousins exist", 278 | %{z_with_1st_cousins: z} do 279 | assert %Zipper{focus: expected} = Zipper.next_first_cousin(z) 280 | 281 | assert %Zipper{focus: actual} = Zipper.next_extended_cousin(z) 282 | 283 | assert actual.term == expected.term 284 | assert actual.term == 25 285 | end 286 | 287 | test "should return the same value matching predicate as Zipper.next_first_cousin/2 when no further extensions exist", 288 | %{ 289 | z_with_1st_cousins: z 290 | } do 291 | predicate = &(&1.term == 29) 292 | 293 | assert %Zipper{focus: expected} = Zipper.next_first_cousin(z, predicate) 294 | 295 | assert %Zipper{focus: actual} = Zipper.next_extended_cousin(z, predicate) 296 | 297 | assert actual.term == expected.term 298 | assert 29 == actual.term 299 | end 300 | 301 | test "should return the same value as Zipper.next_second_cousin/2 when no further extended cousins exist", 302 | %{z_with_2nd_cousins: z} do 303 | assert %Zipper{focus: expected} = Zipper.next_second_cousin(z) 304 | 305 | assert %Zipper{focus: actual} = Zipper.next_extended_cousin(z) 306 | 307 | assert actual.term == expected.term 308 | assert actual.term == 52 309 | end 310 | 311 | test "should return the same value matching predicate as Zipper.next_second_cousin/2 when no further extensions exist", 312 | %{ 313 | z_with_2nd_cousins: z 314 | } do 315 | for target <- 52..58 do 316 | predicate = &(&1.term == target) 317 | 318 | assert %Zipper{focus: expected} = Zipper.next_second_cousin(z, predicate) 319 | 320 | assert %Zipper{focus: actual} = Zipper.next_extended_cousin(z, predicate) 321 | 322 | assert actual.term == expected.term 323 | assert target == actual.term 324 | end 325 | end 326 | 327 | test "should return the next extended cousin found", 328 | %{z_with_extended_cousins: z} do 329 | actual = Zipper.next_extended_cousin(z) 330 | assert 105 == actual.focus.term 331 | end 332 | 333 | test "should return the next extended cousin found matching the predicate", 334 | %{z_with_extended_cousins: z} do 335 | predicate = &(&1.term == 107) 336 | 337 | actual = Zipper.next_extended_cousin(z, predicate) 338 | assert 107 == actual.focus.term 339 | end 340 | 341 | test "should return the next extended cousin found in scenario 2", 342 | %{z_with_extended_cousins_2: z} do 343 | actual = Zipper.next_extended_cousin(z) 344 | assert 29 == actual.focus.term 345 | end 346 | 347 | test "should return the next extended cousin found matching the predicate in scenario 2", 348 | %{z_with_extended_cousins_2: z} do 349 | predicate = &(&1.term == 31) 350 | 351 | actual = Zipper.next_extended_cousin(z, predicate) 352 | assert 31 == actual.focus.term 353 | end 354 | end 355 | end 356 | -------------------------------------------------------------------------------- /test/ex_rose_tree/zipper/first_cousin_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Zipper.FirstCousinTest do 2 | use ExUnit.Case, async: true 3 | use ZipperCase 4 | 5 | describe "first_first_cousin/2" do 6 | test "should return nil if no parent found", %{simple_z: z} do 7 | assert Zipper.first_first_cousin(z) == nil 8 | end 9 | 10 | test "should return nil if parent has no siblings", %{z_with_parent: z} do 11 | assert Zipper.first_first_cousin(z) == nil 12 | end 13 | 14 | test "should return nil if no previous pibling has children", 15 | %{z_with_piblings: z} do 16 | assert Zipper.first_first_cousin(z) == nil 17 | end 18 | 19 | test "should return nil if no first-cousin found matching predicate", 20 | %{z_with_1st_cousins: z} do 21 | predicate = &(&1.term == :not_found) 22 | 23 | assert Zipper.first_first_cousin(z, predicate) == nil 24 | end 25 | 26 | test "should return the first first-cousin found", %{ 27 | z_with_1st_cousins: z 28 | } do 29 | actual = Zipper.first_first_cousin(z) 30 | assert 19 == actual.focus.term 31 | end 32 | 33 | test "should return the first first-cousin matching the predicate", %{ 34 | z_with_1st_cousins: z 35 | } do 36 | predicate = &(&1.term == 23) 37 | 38 | actual = Zipper.first_first_cousin(z, predicate) 39 | assert 23 == actual.focus.term 40 | end 41 | 42 | test "should return nil and not seek past the original parent for a predicate match", %{ 43 | z_with_1st_cousins: z 44 | } do 45 | predicate = &(&1.term == 29) 46 | 47 | assert Zipper.first_first_cousin(z, predicate) == nil 48 | end 49 | end 50 | 51 | describe "last_first_cousin/2" do 52 | test "should return nil if no parent found", %{simple_z: z} do 53 | assert Zipper.last_first_cousin(z) == nil 54 | end 55 | 56 | test "should return nil if parent has no siblings", %{z_with_parent: z} do 57 | assert Zipper.last_first_cousin(z) == nil 58 | end 59 | 60 | test "should return nil if no next pibling has children", 61 | %{z_with_piblings: z} do 62 | assert Zipper.last_first_cousin(z) == nil 63 | end 64 | 65 | test "should return nil if no first-cousin found matching predicate", 66 | %{z_with_1st_cousins: z} do 67 | predicate = &(&1.term == :not_found) 68 | 69 | assert Zipper.last_first_cousin(z, predicate) == nil 70 | end 71 | 72 | test "should return the last first-cousin found", %{ 73 | z_with_1st_cousins: z 74 | } do 75 | actual = Zipper.last_first_cousin(z) 76 | assert 30 == actual.focus.term 77 | end 78 | 79 | test "should return the last first-cousin matching the predicate", %{ 80 | z_with_1st_cousins: z 81 | } do 82 | predicate = &(&1.term == 26) 83 | 84 | actual = Zipper.last_first_cousin(z, predicate) 85 | assert 26 == actual.focus.term 86 | end 87 | 88 | test "should return nil and not seek before the original parent for a predicate match", %{ 89 | z_with_1st_cousins: z 90 | } do 91 | predicate = &(&1.term == 22) 92 | 93 | assert Zipper.last_first_cousin(z, predicate) == nil 94 | end 95 | end 96 | 97 | describe "previous_first_cousin/2" do 98 | test "should return nil if no parent found", %{simple_z: z} do 99 | assert Zipper.previous_first_cousin(z) == nil 100 | end 101 | 102 | test "should return nil if parent has no siblings", %{z_with_parent: z} do 103 | assert Zipper.previous_first_cousin(z) == nil 104 | end 105 | 106 | test "should return nil if no previous pibling has children", 107 | %{z_with_piblings: z} do 108 | assert Zipper.previous_first_cousin(z) == nil 109 | end 110 | 111 | test "should return nil if no first-cousin found matching predicate", 112 | %{z_with_1st_cousins: z} do 113 | predicate = &(&1.term == :not_found) 114 | 115 | assert Zipper.previous_first_cousin(z, predicate) == nil 116 | end 117 | 118 | test "should return the first previous first-cousin found", %{ 119 | z_with_1st_cousins: z 120 | } do 121 | actual = Zipper.previous_first_cousin(z) 122 | assert 24 == actual.focus.term 123 | end 124 | 125 | test "should return the first first-cousin matching the predicate", %{ 126 | z_with_1st_cousins: z 127 | } do 128 | predicate = &(&1.term == 19) 129 | 130 | actual = Zipper.previous_first_cousin(z, predicate) 131 | assert 19 == actual.focus.term 132 | end 133 | 134 | test "should return nil and not seek past the original parent for a predicate match", %{ 135 | z_with_1st_cousins: z 136 | } do 137 | predicate = &(&1.term == 29) 138 | 139 | assert Zipper.previous_first_cousin(z, predicate) == nil 140 | end 141 | end 142 | 143 | describe "next_first_cousin/2" do 144 | test "should return nil if no parent found", %{simple_z: z} do 145 | assert Zipper.next_first_cousin(z) == nil 146 | end 147 | 148 | test "should return nil if parent has no siblings", %{z_with_parent: z} do 149 | assert Zipper.next_first_cousin(z) == nil 150 | end 151 | 152 | test "should return nil if no next pibling has children", 153 | %{z_with_piblings: z} do 154 | assert Zipper.next_first_cousin(z) == nil 155 | end 156 | 157 | test "should return nil if no first-cousin found matching predicate", 158 | %{z_with_1st_cousins: z} do 159 | predicate = &(&1.term == :not_found) 160 | 161 | assert Zipper.next_first_cousin(z, predicate) == nil 162 | end 163 | 164 | test "should return the last first-cousin found", %{ 165 | z_with_1st_cousins: z 166 | } do 167 | actual = Zipper.next_first_cousin(z) 168 | assert 25 == actual.focus.term 169 | end 170 | 171 | test "should return the last first-cousin matching the predicate", %{ 172 | z_with_1st_cousins: z 173 | } do 174 | predicate = &(&1.term == 29) 175 | 176 | actual = Zipper.next_first_cousin(z, predicate) 177 | assert 29 == actual.focus.term 178 | end 179 | 180 | test "should return nil and not seek before the original parent for a predicate match", %{ 181 | z_with_1st_cousins: z 182 | } do 183 | predicate = &(&1.term == 22) 184 | 185 | assert Zipper.next_first_cousin(z, predicate) == nil 186 | end 187 | end 188 | end 189 | -------------------------------------------------------------------------------- /test/ex_rose_tree/zipper/location_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Zipper.LocationTest do 2 | use ExUnit.Case 3 | 4 | require ExRoseTree.Zipper.Location 5 | alias ExRoseTree.Zipper.Location 6 | alias ExRoseTree 7 | 8 | doctest ExRoseTree.Zipper.Location 9 | 10 | @bad_locs [ 11 | {%{prev: [], term: nil, next: []}, 0}, 12 | {true, 1}, 13 | {false, 2}, 14 | {5, 3}, 15 | {"a word", 4}, 16 | {6.4, 5}, 17 | {:a_thing, 6}, 18 | {[1, 2, 3], 7}, 19 | {{1, 2}, 8}, 20 | {%Location{prev: 5, term: 6, next: [7]}, 9}, 21 | {nil, 10} 22 | ] 23 | 24 | @loc_values [ 25 | {%{a: "value"}, 0}, 26 | {true, 1}, 27 | {false, 2}, 28 | {5, 3}, 29 | {"a word", 4}, 30 | {6.4, 5}, 31 | {:a_thing, 6}, 32 | {[1, 2, 3], 7}, 33 | {{1, 2}, 8} 34 | ] 35 | 36 | describe "location?/1 guard" do 37 | test "should return true when given an empty Location struct" do 38 | loc = %Location{ 39 | prev: [], 40 | term: nil, 41 | next: [] 42 | } 43 | 44 | assert Location.location?(loc) == true 45 | end 46 | 47 | test "should return true when given a valid Location struct" do 48 | loc = %Location{ 49 | prev: [ExRoseTree.new(5)], 50 | term: 6, 51 | next: [ExRoseTree.new(7)] 52 | } 53 | 54 | assert Location.location?(loc) == true 55 | end 56 | 57 | test "should return false when given bad values" do 58 | for {value, idx} <- @bad_locs do 59 | assert Location.location?(value) == false, 60 | "Expected `false` for element at index #{idx}" 61 | end 62 | end 63 | end 64 | 65 | describe "new/2 when using only the first parameter" do 66 | test "should return a new Location for any valid erlang term" do 67 | for {value, idx} <- @loc_values do 68 | loc = Location.new(value) 69 | 70 | assert Location.location?(loc) == true, 71 | "Expected a valid Location struct for element at index #{idx}" 72 | 73 | assert %Location{prev: [], term: ^value, next: []} = loc, 74 | "Expected term to be #{inspect(value)} and prev and next to be empty lists for element at index #{idx}" 75 | end 76 | end 77 | end 78 | 79 | describe "new/2 when using the :prev option" do 80 | test "should return a new Location with prev field set if valid ExRoseTrees were passed" do 81 | prev = [ExRoseTree.new(5)] 82 | 83 | loc = Location.new(6, prev: prev) 84 | 85 | assert Location.location?(loc) == true 86 | assert %Location{prev: ^prev, term: 6, next: []} = loc 87 | end 88 | 89 | test "should return a new Location with next field set if valid ExRoseTrees were passed" do 90 | next = [ExRoseTree.new(7)] 91 | 92 | loc = Location.new(6, next: next) 93 | 94 | assert Location.location?(loc) == true 95 | assert %Location{prev: [], term: 6, next: ^next} = loc 96 | end 97 | 98 | test "should return a new Location with both prev and next fields set if valid ExRoseTrees were passed" do 99 | prev = [ExRoseTree.new(5)] 100 | next = [ExRoseTree.new(7)] 101 | 102 | loc = Location.new(6, prev: prev, next: next) 103 | 104 | assert Location.location?(loc) == true 105 | assert %Location{prev: ^prev, term: 6, next: ^next} = loc 106 | end 107 | 108 | test "should raise an ArgumentError if prev field set with an invalid element" do 109 | prev = [ExRoseTree.new(5), :bad_element] 110 | 111 | assert_raise ArgumentError, fn -> Location.new(6, prev: prev) end 112 | end 113 | 114 | test "should raise an ArgumentError if next field set with an invalid element" do 115 | next = [ExRoseTree.new(7), :bad_element] 116 | 117 | assert_raise ArgumentError, fn -> Location.new(6, next: next) end 118 | end 119 | end 120 | end 121 | -------------------------------------------------------------------------------- /test/ex_rose_tree/zipper/nibling_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Zipper.Zipper.NiblingTest do 2 | use ExUnit.Case, async: true 3 | use ZipperCase 4 | 5 | describe "first_nibling/2" do 6 | test "should return nil if no siblings found", %{simple_z: z} do 7 | assert Zipper.first_nibling(z) == nil 8 | end 9 | 10 | test "should return nil if no siblings with children are found", 11 | %{z_with_siblings: z} do 12 | assert Zipper.first_nibling(z) == nil 13 | end 14 | 15 | test "should return nil if no previous nibling matching the predicate is found", 16 | %{z_with_niblings: z} do 17 | predicate = &(&1.term == :not_found) 18 | 19 | assert Zipper.first_nibling(z, predicate) == nil 20 | end 21 | 22 | test "should return the first nibling", %{ 23 | z_with_niblings: z 24 | } do 25 | actual = Zipper.first_nibling(z) 26 | assert 10 == actual.focus.term 27 | end 28 | 29 | test "should return the first nibling that matches the predicate", %{ 30 | z_with_niblings: z 31 | } do 32 | predicate = &(&1.term == 11) 33 | 34 | actual = Zipper.first_nibling(z, predicate) 35 | assert 11 == actual.focus.term 36 | end 37 | end 38 | 39 | describe "last_nibling/2" do 40 | test "should return nil if no siblings found", %{simple_z: z} do 41 | assert Zipper.last_nibling(z) == nil 42 | end 43 | 44 | test "should return nil if no siblings with children are found", 45 | %{z_with_siblings: z} do 46 | assert Zipper.last_nibling(z) == nil 47 | end 48 | 49 | test "should return nil if no next nibling matching the predicate is found", 50 | %{z_with_niblings: z} do 51 | predicate = &(&1.term == :not_found) 52 | 53 | assert Zipper.last_nibling(z, predicate) == nil 54 | end 55 | 56 | test "should return the last nibling", %{ 57 | z_with_niblings: z 58 | } do 59 | actual = Zipper.last_nibling(z) 60 | assert 15 == actual.focus.term 61 | end 62 | 63 | test "should return the last nibling that matches the predicate", %{ 64 | z_with_niblings: z 65 | } do 66 | predicate = &(&1.term == 14) 67 | 68 | actual = Zipper.last_nibling(z, predicate) 69 | assert 14 == actual.focus.term 70 | end 71 | end 72 | 73 | describe "previous_nibling/2" do 74 | test "should return nil if no siblings found", %{simple_z: z} do 75 | assert Zipper.previous_nibling(z) == nil 76 | end 77 | 78 | test "should return nil if no siblings with children are found", 79 | %{z_with_siblings: z} do 80 | assert Zipper.previous_nibling(z) == nil 81 | end 82 | 83 | test "should return nil if no previous nibling matching the predicate is found", 84 | %{z_with_niblings: z} do 85 | predicate = &(&1.term == :not_found) 86 | 87 | assert Zipper.previous_nibling(z, predicate) == nil 88 | end 89 | 90 | test "should return the previous nibling", %{ 91 | z_with_niblings: z 92 | } do 93 | actual = Zipper.previous_nibling(z) 94 | assert 12 == actual.focus.term 95 | end 96 | 97 | test "should return the previous nibling that matches the predicate", %{ 98 | z_with_niblings: z 99 | } do 100 | predicate = &(&1.term == 11) 101 | 102 | actual = Zipper.previous_nibling(z, predicate) 103 | assert 11 == actual.focus.term 104 | end 105 | end 106 | 107 | describe "next_nibling/2" do 108 | test "should return nil if no siblings found", %{simple_z: z} do 109 | assert Zipper.next_nibling(z) == nil 110 | end 111 | 112 | test "should return nil if no siblings with children are found", 113 | %{z_with_siblings: z} do 114 | assert Zipper.next_nibling(z) == nil 115 | end 116 | 117 | test "should return nil if no next nibling matching the predicate is found", 118 | %{z_with_niblings: z} do 119 | predicate = &(&1.term == :not_found) 120 | 121 | assert Zipper.next_nibling(z, predicate) == nil 122 | end 123 | 124 | test "should return the next nibling", %{ 125 | z_with_niblings: z 126 | } do 127 | actual = Zipper.next_nibling(z) 128 | assert 13 == actual.focus.term 129 | end 130 | 131 | test "should return the next nibling matching the predicate", %{ 132 | z_with_niblings: z 133 | } do 134 | predicate = &(&1.term == 14) 135 | 136 | actual = Zipper.next_nibling(z, predicate) 137 | assert 14 == actual.focus.term 138 | end 139 | end 140 | 141 | describe "first_nibling_at_sibling/3" do 142 | test "should return nil if no siblings found", %{simple_z: z} do 143 | assert Zipper.first_nibling_at_sibling(z, 3) == nil 144 | end 145 | 146 | test "should return nil if no siblings with children are found", 147 | %{z_with_siblings: z} do 148 | assert Zipper.first_nibling_at_sibling(z, 3) == nil 149 | end 150 | 151 | test "should return nil when given an index that is out of bounds for siblings", 152 | %{z_with_niblings: z} do 153 | num_siblings = Enum.count(z.prev) + Enum.count(z.next) + 1 154 | 155 | for _ <- 0..5 do 156 | idx = Enum.random(num_siblings..20) 157 | assert Zipper.first_nibling_at_sibling(z, idx) == nil 158 | end 159 | end 160 | 161 | test "should return nil when given an index that matches the current Zipper's index", 162 | %{z_with_niblings: z} do 163 | current_idx = Enum.count(z.prev) 164 | 165 | assert Zipper.first_nibling_at_sibling(z, current_idx) == nil 166 | end 167 | 168 | test "should return nil if no previous nibling matching the predicate is found for the sibling at index", 169 | %{z_with_niblings: z} do 170 | predicate = &(&1.term == :not_found) 171 | 172 | assert Zipper.first_nibling_at_sibling(z, 6, predicate) == nil 173 | end 174 | 175 | test "should return the first nibling for sibling at index", %{ 176 | z_with_niblings: z 177 | } do 178 | actual = Zipper.first_nibling_at_sibling(z, 6) 179 | assert 13 == actual.focus.term 180 | end 181 | 182 | test "should return the first nibling that matches the predicate", %{ 183 | z_with_niblings: z 184 | } do 185 | predicate = &(&1.term == 14) 186 | 187 | actual = Zipper.first_nibling_at_sibling(z, 6, predicate) 188 | assert 14 == actual.focus.term 189 | end 190 | end 191 | 192 | describe "last_nibling_at_sibling/3" do 193 | test "should return nil if no siblings are found", %{simple_z: z} do 194 | assert Zipper.last_nibling_at_sibling(z, 7) == nil 195 | end 196 | 197 | test "should return nil if no siblings with children are found", 198 | %{z_with_siblings: z} do 199 | assert Zipper.last_nibling_at_sibling(z, 7) == nil 200 | end 201 | 202 | test "should return nil when given an index that is out of bounds for siblings", 203 | %{z_with_niblings: z} do 204 | num_siblings = Enum.count(z.prev) + Enum.count(z.next) + 1 205 | 206 | for _ <- 0..5 do 207 | idx = Enum.random(num_siblings..20) 208 | assert Zipper.last_nibling_at_sibling(z, idx) == nil 209 | end 210 | end 211 | 212 | test "should return nil when given an index that matches the current Zipper's index", 213 | %{z_with_niblings: z} do 214 | current_idx = Enum.count(z.prev) 215 | 216 | assert Zipper.last_nibling_at_sibling(z, current_idx) == nil 217 | end 218 | 219 | test "should return nil if no next nibling matching the predicate is found for the sibling at index", 220 | %{z_with_niblings: z} do 221 | predicate = &(&1.term == :not_found) 222 | 223 | assert Zipper.last_nibling_at_sibling(z, 2, predicate) == nil 224 | end 225 | 226 | test "should return the last nibling for sibling at index", %{ 227 | z_with_niblings: z 228 | } do 229 | actual = Zipper.last_nibling_at_sibling(z, 2) 230 | assert 12 == actual.focus.term 231 | end 232 | 233 | test "should return the last nibling matching the predicate", %{ 234 | z_with_niblings: z 235 | } do 236 | predicate = &(&1.term == 10) 237 | 238 | actual = Zipper.last_nibling_at_sibling(z, 2, predicate) 239 | assert 10 == actual.focus.term 240 | end 241 | end 242 | 243 | describe "previous_grandnibling/2" do 244 | test "should return nil if no siblings found", %{simple_z: z} do 245 | assert Zipper.previous_grandnibling(z) == nil 246 | end 247 | 248 | test "should return nil if no siblings with children are found", 249 | %{z_with_siblings: z} do 250 | assert Zipper.previous_grandnibling(z) == nil 251 | end 252 | 253 | test "should return nil if no siblings with grandchildren are found", 254 | %{z_with_niblings: z} do 255 | assert Zipper.previous_grandnibling(z) == nil 256 | end 257 | 258 | test "should return nil if no previous grandnibling matching the predicate is found", 259 | %{z_with_grand_niblings: z} do 260 | predicate = &(&1.term == :not_found) 261 | 262 | assert Zipper.previous_grandnibling(z, predicate) == nil 263 | end 264 | 265 | test "should return the previous grandnibling", %{ 266 | z_with_grand_niblings: z 267 | } do 268 | actual = Zipper.previous_grandnibling(z) 269 | assert 20 == actual.focus.term 270 | end 271 | 272 | test "should return the previous grandnibling matching the predicate", %{ 273 | z_with_grand_niblings: z 274 | } do 275 | predicate = &(&1.term == 21) 276 | 277 | actual = Zipper.previous_grandnibling(z, predicate) 278 | assert 21 == actual.focus.term 279 | end 280 | end 281 | 282 | describe "next_grandnibling/2" do 283 | test "should return nil if no siblings found", %{simple_z: z} do 284 | assert Zipper.next_grandnibling(z) == nil 285 | end 286 | 287 | test "should return nil if no siblings with children are found", 288 | %{z_with_siblings: z} do 289 | assert Zipper.next_grandnibling(z) == nil 290 | end 291 | 292 | test "should return nil if no siblings with grandchildren are found", 293 | %{z_with_niblings: z} do 294 | assert Zipper.next_grandnibling(z) == nil 295 | end 296 | 297 | test "should return nil if no next grandnibling matching the predicate is found", 298 | %{z_with_grand_niblings: z} do 299 | predicate = &(&1.term == :not_found) 300 | 301 | assert Zipper.next_grandnibling(z, predicate) == nil 302 | end 303 | 304 | test "should return the next grandnibling", %{ 305 | z_with_grand_niblings: z 306 | } do 307 | actual = Zipper.next_grandnibling(z) 308 | assert 26 == actual.focus.term 309 | end 310 | 311 | test "should return the next grandnibling matching the predicate", %{ 312 | z_with_grand_niblings: z 313 | } do 314 | predicate = &(&1.term == 30) 315 | 316 | actual = Zipper.next_grandnibling(z, predicate) 317 | assert 30 == actual.focus.term 318 | end 319 | end 320 | 321 | describe "first_descendant_nibling/2" do 322 | test "should return nil if no previous sibling found", %{simple_z: z} do 323 | assert Zipper.first_descendant_nibling(z) == nil 324 | end 325 | 326 | test "should return nil if first previous sibling has no children", %{ 327 | z_with_niblings: z 328 | } do 329 | assert Zipper.first_descendant_nibling(z) == nil 330 | end 331 | 332 | test "should return nil if no previous descendant nibling, starting from first, found matching predicate", 333 | %{ 334 | z_with_descendant_niblings: z 335 | } do 336 | predicate = &(&1.focus.term == :not_found) 337 | 338 | assert Zipper.first_descendant_nibling(z, predicate) == nil 339 | end 340 | 341 | test "should return the first descendant nibling found", %{ 342 | z_with_descendant_niblings: z 343 | } do 344 | assert %Zipper{focus: focus} = Zipper.first_descendant_nibling(z) 345 | assert 300 == focus.term 346 | end 347 | 348 | test "should return the first previous descendant nibling, starting from first, found matching the predicate", 349 | %{ 350 | z_with_descendant_niblings: z 351 | } do 352 | predicate = &(&1.focus.term == 200) 353 | 354 | assert %Zipper{focus: focus} = Zipper.first_descendant_nibling(z, predicate) 355 | assert 200 == focus.term 356 | end 357 | end 358 | 359 | describe "last_descendant_nibling/2" do 360 | test "should return nil if no previous sibling found", %{simple_z: z} do 361 | assert Zipper.last_descendant_nibling(z) == nil 362 | end 363 | 364 | test "should return nil if first previous sibling has no children", %{ 365 | z_with_niblings: z 366 | } do 367 | assert Zipper.last_descendant_nibling(z) == nil 368 | end 369 | 370 | test "should return nil if no previous descendant nibling, starting from first, found matching predicate", 371 | %{ 372 | z_with_descendant_niblings: z 373 | } do 374 | predicate = &(&1.focus.term == :not_found) 375 | 376 | assert Zipper.last_descendant_nibling(z, predicate) == nil 377 | end 378 | 379 | test "should return the first descendant nibling found", %{ 380 | z_with_descendant_niblings: z 381 | } do 382 | assert %Zipper{focus: focus} = Zipper.last_descendant_nibling(z) 383 | assert 901 == focus.term 384 | end 385 | 386 | test "should return the first previous descendant nibling, starting from first, found matching the predicate", 387 | %{ 388 | z_with_descendant_niblings: z 389 | } do 390 | predicate = &(&1.focus.term == 701) 391 | 392 | assert %Zipper{focus: focus} = Zipper.last_descendant_nibling(z, predicate) 393 | assert 701 == focus.term 394 | end 395 | end 396 | 397 | describe "previous_descendant_nibling/2" do 398 | test "should return nil if no previous sibling found", %{simple_z: z} do 399 | assert Zipper.previous_descendant_nibling(z) == nil 400 | end 401 | 402 | test "should return nil if immediately previous sibling has no children", %{ 403 | z_with_niblings: z 404 | } do 405 | assert Zipper.previous_descendant_nibling(z) == nil 406 | end 407 | 408 | test "should return nil if no previous descendant nibling found matching predicate", %{ 409 | z_with_descendant_niblings: z 410 | } do 411 | predicate = &(&1.focus.term == :not_found) 412 | 413 | assert Zipper.previous_descendant_nibling(z, predicate) == nil 414 | end 415 | 416 | test "should return the last previous descendant nibling found", %{ 417 | z_with_descendant_niblings: z 418 | } do 419 | actual = Zipper.previous_descendant_nibling(z) 420 | assert 25 == actual.focus.term 421 | end 422 | 423 | test "should return the last previous descendant nibling found matching the predicate", %{ 424 | z_with_descendant_niblings: z 425 | } do 426 | predicate = &(&1.focus.term == 12) 427 | 428 | assert %Zipper{focus: focus} = Zipper.previous_descendant_nibling(z, predicate) 429 | assert 12 == focus.term 430 | end 431 | end 432 | 433 | describe "next_descendant_nibling/2" do 434 | test "should return nil if no next sibling found", %{simple_z: z} do 435 | assert Zipper.next_descendant_nibling(z) == nil 436 | end 437 | 438 | test "should return nil if immediately next sibling has no children", %{ 439 | z_with_niblings: z 440 | } do 441 | assert Zipper.next_descendant_nibling(z) == nil 442 | end 443 | 444 | test "should return nil if no next descendant nibling found matching predicate", %{ 445 | z_with_descendant_niblings: z 446 | } do 447 | predicate = &(&1.focus.term == :not_found) 448 | 449 | assert Zipper.next_descendant_nibling(z, predicate) == nil 450 | end 451 | 452 | test "should return the last next descendant nibling found", %{ 453 | z_with_descendant_niblings: z 454 | } do 455 | actual = Zipper.next_descendant_nibling(z) 456 | assert 37 == actual.focus.term 457 | end 458 | 459 | test "should return the last next descendant nibling found matching the predicate", %{ 460 | z_with_descendant_niblings: z 461 | } do 462 | predicate = &(&1.focus.term == 29) 463 | 464 | assert %Zipper{focus: focus} = Zipper.next_descendant_nibling(z, predicate) 465 | assert 29 == focus.term 466 | end 467 | end 468 | 469 | describe "first_extended_nibling/2" do 470 | test "should return nil if no parent found", %{simple_z: z} do 471 | assert Zipper.first_extended_nibling(z) == nil 472 | end 473 | 474 | test "should return nil if no grandparent found", %{z_with_parent: z} do 475 | assert Zipper.first_extended_nibling(z) == nil 476 | end 477 | 478 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 479 | assert Zipper.first_extended_nibling(z) == nil 480 | end 481 | 482 | test "should return nil if no previous grandpibling has children", 483 | %{z_with_grandpiblings: z} do 484 | assert Zipper.first_extended_nibling(z) == nil 485 | end 486 | 487 | test "should return nil if no extended nibling found matching predicate", 488 | %{ 489 | z_with_extended_niblings: z 490 | } do 491 | predicate = &(&1.term == :not_found) 492 | 493 | assert Zipper.first_extended_nibling(z, predicate) == nil 494 | end 495 | 496 | test "should return the first extended nibling found", 497 | %{z_with_extended_niblings: z} do 498 | assert %Zipper{focus: actual} = Zipper.first_extended_nibling(z) 499 | assert 202 == actual.term 500 | end 501 | 502 | test "should return the first extended nibling found matching the predicate", 503 | %{z_with_extended_niblings: z} do 504 | predicate = &(&1.term == 203) 505 | 506 | assert %Zipper{focus: actual} = Zipper.first_extended_nibling(z, predicate) 507 | assert 203 == actual.term 508 | end 509 | end 510 | 511 | describe "last_extended_nibling/2" do 512 | test "should return nil if no parent found", %{simple_z: z} do 513 | assert Zipper.last_extended_nibling(z) == nil 514 | end 515 | 516 | test "should return nil if no grandparent found", %{z_with_parent: z} do 517 | assert Zipper.last_extended_nibling(z) == nil 518 | end 519 | 520 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 521 | assert Zipper.last_extended_nibling(z) == nil 522 | end 523 | 524 | test "should return nil if no next grandpibling has children", 525 | %{z_with_grandpiblings: z} do 526 | assert Zipper.last_extended_nibling(z) == nil 527 | end 528 | 529 | test "should return nil if no extended nibling found matching predicate", 530 | %{ 531 | z_with_extended_niblings: z 532 | } do 533 | predicate = &(&1.term == :not_found) 534 | 535 | assert Zipper.last_extended_nibling(z, predicate) == nil 536 | end 537 | 538 | test "should return the last extended nibling found", 539 | %{z_with_extended_niblings: z} do 540 | assert %Zipper{focus: actual} = Zipper.last_extended_nibling(z) 541 | assert 209 == actual.term 542 | end 543 | 544 | test "should return the last extended nibling found matching the predicate", 545 | %{z_with_extended_niblings: z} do 546 | predicate = &(&1.term == 208) 547 | 548 | assert %Zipper{focus: actual} = Zipper.last_extended_nibling(z, predicate) 549 | assert 208 == actual.term 550 | end 551 | end 552 | 553 | describe "previous_extended_nibling/2" do 554 | test "should return nil if no parent found", %{simple_z: z} do 555 | assert Zipper.previous_extended_nibling(z) == nil 556 | end 557 | 558 | test "should return nil if no grandparent found", %{z_with_parent: z} do 559 | assert Zipper.previous_extended_nibling(z) == nil 560 | end 561 | 562 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 563 | assert Zipper.previous_extended_nibling(z) == nil 564 | end 565 | 566 | test "should return nil if no previous grandpibling has children", 567 | %{z_with_grandpiblings: z} do 568 | assert Zipper.previous_extended_nibling(z) == nil 569 | end 570 | 571 | test "should return nil if no extended nibling found matching predicate", 572 | %{ 573 | z_with_extended_niblings: z 574 | } do 575 | predicate = &(&1.term == :not_found) 576 | 577 | assert Zipper.previous_extended_nibling(z, predicate) == nil 578 | end 579 | 580 | test "should return the previous extended nibling found", 581 | %{z_with_extended_niblings: z} do 582 | assert %Zipper{focus: actual} = Zipper.previous_extended_nibling(z) 583 | assert 201 == actual.term 584 | end 585 | 586 | test "should return the previous extended nibling found matching the predicate", 587 | %{z_with_extended_niblings: z} do 588 | predicate = &(&1.term == 200) 589 | 590 | assert %Zipper{focus: actual} = Zipper.previous_extended_nibling(z, predicate) 591 | assert 200 == actual.term 592 | end 593 | end 594 | 595 | describe "next_extended_nibling/2" do 596 | test "should return nil if no parent found", %{simple_z: z} do 597 | assert Zipper.next_extended_nibling(z) == nil 598 | end 599 | 600 | test "should return nil if no grandparent found", %{z_with_parent: z} do 601 | assert Zipper.next_extended_nibling(z) == nil 602 | end 603 | 604 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 605 | assert Zipper.next_extended_nibling(z) == nil 606 | end 607 | 608 | test "should return nil if no previous grandpibling has children", 609 | %{z_with_grandpiblings: z} do 610 | assert Zipper.next_extended_nibling(z) == nil 611 | end 612 | 613 | test "should return nil if no extended nibling found matching predicate", 614 | %{ 615 | z_with_extended_niblings: z 616 | } do 617 | predicate = &(&1.term == :not_found) 618 | 619 | assert Zipper.next_extended_nibling(z, predicate) == nil 620 | end 621 | 622 | test "should return the previous extended nibling found", 623 | %{z_with_extended_niblings: z} do 624 | assert %Zipper{focus: actual} = Zipper.next_extended_nibling(z) 625 | assert 204 == actual.term 626 | end 627 | 628 | test "should return the previous extended nibling found matching the predicate", 629 | %{z_with_extended_niblings: z} do 630 | predicate = &(&1.term == 206) 631 | 632 | assert %Zipper{focus: actual} = Zipper.next_extended_nibling(z, predicate) 633 | assert 206 == actual.term 634 | end 635 | end 636 | end 637 | -------------------------------------------------------------------------------- /test/ex_rose_tree/zipper/pibling_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Zipper.PiblingTest do 2 | use ExUnit.Case, async: true 3 | use ZipperCase 4 | 5 | describe "first_pibling/2" do 6 | test "should return nil if no parent found", %{simple_z: z} do 7 | assert Zipper.first_pibling(z) == nil 8 | end 9 | 10 | test "should return nil if parent has no siblings", %{z_with_parent: z} do 11 | assert Zipper.first_pibling(z) == nil 12 | end 13 | 14 | test "should return nil if no previous pibling found matching the predicate", 15 | %{z_with_piblings: z} do 16 | predicate = &(&1.term == :not_found) 17 | 18 | assert Zipper.first_pibling(z, predicate) == nil 19 | end 20 | 21 | test "should return the first pibling found", %{ 22 | z_with_piblings: z 23 | } do 24 | actual = Zipper.first_pibling(z) 25 | assert 2 == actual.focus.term 26 | end 27 | 28 | test "should return the first first pibling matching the predicate", %{ 29 | z_with_piblings: z 30 | } do 31 | predicate = &(&1.term == 4) 32 | 33 | actual = Zipper.first_pibling(z, predicate) 34 | assert 4 == actual.focus.term 35 | end 36 | 37 | test "should return nil and not seek past the original parent for a predicate match", %{ 38 | z_with_piblings: z 39 | } do 40 | predicate = &(&1.term == 14) 41 | 42 | assert Zipper.first_pibling(z, predicate) == nil 43 | end 44 | end 45 | 46 | describe "last_pibling/2" do 47 | test "should return nil if no parent found", %{simple_z: z} do 48 | assert Zipper.last_pibling(z) == nil 49 | end 50 | 51 | test "should return nil if parent has no siblings", %{z_with_parent: z} do 52 | assert Zipper.last_pibling(z) == nil 53 | end 54 | 55 | test "should return nil if no next pibling found matching the predicate", 56 | %{z_with_piblings: z} do 57 | predicate = &(&1.term == :not_found) 58 | 59 | assert Zipper.last_pibling(z, predicate) == nil 60 | end 61 | 62 | test "should return the last pibling found", %{ 63 | z_with_piblings: z 64 | } do 65 | actual = Zipper.last_pibling(z) 66 | assert 18 == actual.focus.term 67 | end 68 | 69 | test "should return the first last pibling matching the predicate", %{ 70 | z_with_piblings: z 71 | } do 72 | predicate = &(&1.term == 14) 73 | 74 | actual = Zipper.last_pibling(z, predicate) 75 | assert 14 == actual.focus.term 76 | end 77 | 78 | test "should return nil and not seek before the original parent for a predicate match", %{ 79 | z_with_piblings: z 80 | } do 81 | predicate = &(&1.term == 6) 82 | 83 | assert Zipper.last_pibling(z, predicate) == nil 84 | end 85 | end 86 | 87 | describe "previous_pibling/2" do 88 | test "should return nil if no parent found", %{simple_z: z} do 89 | assert Zipper.previous_pibling(z) == nil 90 | end 91 | 92 | test "should return nil if parent has no siblings", %{z_with_parent: z} do 93 | assert Zipper.previous_pibling(z) == nil 94 | end 95 | 96 | test "should return nil if no previous pibling found matching the predicate", 97 | %{z_with_piblings: z} do 98 | predicate = &(&1.term == :not_found) 99 | 100 | assert Zipper.previous_pibling(z, predicate) == nil 101 | end 102 | 103 | test "should return the first previous pibling found", %{ 104 | z_with_piblings: z 105 | } do 106 | actual = Zipper.previous_pibling(z) 107 | assert 6 == actual.focus.term 108 | end 109 | 110 | test "should return the first previous pibling matching the predicate", %{ 111 | z_with_piblings: z 112 | } do 113 | predicate = &(&1.term == 4) 114 | 115 | actual = Zipper.previous_pibling(z, predicate) 116 | assert 4 == actual.focus.term 117 | end 118 | end 119 | 120 | describe "next_pibling/2" do 121 | test "should return nil if no parent found", %{simple_z: z} do 122 | assert Zipper.next_pibling(z) == nil 123 | end 124 | 125 | test "should return nil if parent has no siblings", %{z_with_parent: z} do 126 | assert Zipper.next_pibling(z) == nil 127 | end 128 | 129 | test "should return nil if no next pibling found matching the predicate", 130 | %{z_with_piblings: z} do 131 | predicate = &(&1.term == :not_found) 132 | 133 | assert Zipper.next_pibling(z, predicate) == nil 134 | end 135 | 136 | test "should return the first next pibling found", %{ 137 | z_with_piblings: z 138 | } do 139 | actual = Zipper.next_pibling(z) 140 | assert 14 == actual.focus.term 141 | end 142 | 143 | test "should return the first next pibling matching the predicate", %{ 144 | z_with_piblings: z 145 | } do 146 | predicate = &(&1.term == 18) 147 | 148 | actual = Zipper.next_pibling(z, predicate) 149 | assert 18 == actual.focus.term 150 | end 151 | end 152 | 153 | describe "pibling_at/2" do 154 | test "should return nil when parent has no siblings", %{simple_z: z} do 155 | for _ <- 0..5 do 156 | idx = Enum.random(0..10) 157 | assert Zipper.pibling_at(z, idx) == nil 158 | end 159 | end 160 | 161 | test "should return nil when given an index that is out of bounds for the parent's siblings", 162 | %{z_with_piblings: z} do 163 | [parent | _] = z.path 164 | 165 | num_piblings = Enum.count(parent.prev) + Enum.count(parent.next) + 1 166 | 167 | for _ <- 0..5 do 168 | idx = Enum.random(num_piblings..20) 169 | assert Zipper.pibling_at(z, idx) == nil 170 | end 171 | end 172 | 173 | test "should return nil when given an index that matches the current Zipper's index", 174 | %{z_with_piblings: z} do 175 | [parent | _] = z.path 176 | 177 | current_idx = Enum.count(parent.prev) 178 | 179 | assert Zipper.pibling_at(z, current_idx) == nil 180 | end 181 | 182 | test "should return the pibling at the given index", %{z_with_piblings: z} do 183 | actual = Zipper.pibling_at(z, 0) 184 | assert 2 == actual.focus.term 185 | end 186 | end 187 | 188 | describe "first_grandpibling/2" do 189 | test "should return nil if no parent found", %{simple_z: z} do 190 | assert Zipper.first_grandpibling(z) == nil 191 | end 192 | 193 | test "should return nil if no grandparent found", %{z_with_parent: z} do 194 | assert Zipper.first_grandpibling(z) == nil 195 | end 196 | 197 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 198 | assert Zipper.first_grandpibling(z) == nil 199 | end 200 | 201 | test "should return nil if no previous grandpibling found matching the predicate", 202 | %{z_with_grandpiblings: z} do 203 | predicate = &(&1.term == :not_found) 204 | 205 | assert Zipper.first_grandpibling(z, predicate) == nil 206 | end 207 | 208 | test "should return the first grandpibling found", %{ 209 | z_with_grandpiblings: z 210 | } do 211 | actual = Zipper.first_grandpibling(z) 212 | assert 2 == actual.focus.term 213 | end 214 | 215 | test "should return the first first grandpibling matching the predicate", %{ 216 | z_with_grandpiblings: z 217 | } do 218 | predicate = &(&1.term == 3) 219 | 220 | actual = Zipper.first_grandpibling(z, predicate) 221 | assert 3 == actual.focus.term 222 | end 223 | 224 | test "should return nil and not seek past the original grandparent for a predicate match", %{ 225 | z_with_grandpiblings: z 226 | } do 227 | predicate = &(&1.term == 6) 228 | 229 | assert Zipper.first_grandpibling(z, predicate) == nil 230 | end 231 | end 232 | 233 | describe "last_grandpibling/2" do 234 | test "should return nil if no parent found", %{simple_z: z} do 235 | assert Zipper.last_grandpibling(z) == nil 236 | end 237 | 238 | test "should return nil if no grandparent found", %{z_with_parent: z} do 239 | assert Zipper.last_grandpibling(z) == nil 240 | end 241 | 242 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 243 | assert Zipper.last_grandpibling(z) == nil 244 | end 245 | 246 | test "should return nil if no previous grandpibling found matching the predicate", 247 | %{z_with_grandpiblings: z} do 248 | predicate = &(&1.term == :not_found) 249 | 250 | assert Zipper.last_grandpibling(z, predicate) == nil 251 | end 252 | 253 | test "should return the last grandpibling found", %{ 254 | z_with_grandpiblings: z 255 | } do 256 | actual = Zipper.last_grandpibling(z) 257 | assert 8 == actual.focus.term 258 | end 259 | 260 | test "should return the first last grandpibling matching the predicate", %{ 261 | z_with_grandpiblings: z 262 | } do 263 | predicate = &(&1.term == 7) 264 | 265 | actual = Zipper.last_grandpibling(z, predicate) 266 | assert 7 == actual.focus.term 267 | end 268 | 269 | test "should return nil and not seek before the original grandparent for a predicate match", 270 | %{ 271 | z_with_grandpiblings: z 272 | } do 273 | predicate = &(&1.term == 3) 274 | 275 | assert Zipper.last_grandpibling(z, predicate) == nil 276 | end 277 | end 278 | 279 | describe "previous_grandpibling/2" do 280 | test "should return nil if no parent found", %{simple_z: z} do 281 | assert Zipper.previous_grandpibling(z) == nil 282 | end 283 | 284 | test "should return nil if no grandparent found", %{z_with_parent: z} do 285 | assert Zipper.previous_grandpibling(z) == nil 286 | end 287 | 288 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 289 | assert Zipper.previous_grandpibling(z) == nil 290 | end 291 | 292 | test "should return nil if no previous grandpibling found matching the predicate", 293 | %{z_with_grandpiblings: z} do 294 | predicate = &(&1.term == :not_found) 295 | 296 | assert Zipper.previous_grandpibling(z, predicate) == nil 297 | end 298 | 299 | test "should return the previous grandpibling found", %{ 300 | z_with_grandpiblings: z 301 | } do 302 | actual = Zipper.previous_grandpibling(z) 303 | assert 4 == actual.focus.term 304 | end 305 | 306 | test "should return the first previous grandpibling matching the predicate", %{ 307 | z_with_grandpiblings: z 308 | } do 309 | predicate = &(&1.term == 3) 310 | 311 | actual = Zipper.previous_grandpibling(z, predicate) 312 | assert 3 == actual.focus.term 313 | end 314 | end 315 | 316 | describe "next_grandpibling/2" do 317 | test "should return nil if no parent found", %{simple_z: z} do 318 | assert Zipper.next_grandpibling(z) == nil 319 | end 320 | 321 | test "should return nil if no grandparent found", %{z_with_parent: z} do 322 | assert Zipper.next_grandpibling(z) == nil 323 | end 324 | 325 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 326 | assert Zipper.next_grandpibling(z) == nil 327 | end 328 | 329 | test "should return nil if no next grandpibling found matching the predicate", 330 | %{z_with_grandpiblings: z} do 331 | predicate = &(&1.term == :not_found) 332 | 333 | assert Zipper.next_grandpibling(z, predicate) == nil 334 | end 335 | 336 | test "should return the next grandpibling found", %{ 337 | z_with_grandpiblings: z 338 | } do 339 | actual = Zipper.next_grandpibling(z) 340 | assert 6 == actual.focus.term 341 | end 342 | 343 | test "should return the first next grandpibling matching the predicate", %{ 344 | z_with_grandpiblings: z 345 | } do 346 | predicate = &(&1.term == 7) 347 | 348 | actual = Zipper.next_grandpibling(z, predicate) 349 | assert 7 == actual.focus.term 350 | end 351 | end 352 | 353 | describe "first_ancestral_pibling/2" do 354 | test "should return nil if no parent found", %{simple_z: z} do 355 | assert Zipper.first_ancestral_pibling(z) == nil 356 | end 357 | 358 | test "should return nil if no ancestors have siblings", %{z_with_no_ancestral_piblings: z} do 359 | assert Zipper.first_ancestral_pibling(z) == nil 360 | end 361 | 362 | test "should return nil if no previous pibling for any ancestor found matching the predicate", 363 | %{ 364 | z_with_piblings: z_1, 365 | z_with_grandpiblings: z_2, 366 | z_with_ancestral_piblings: z_3 367 | } do 368 | predicate = &(&1.term == :not_found) 369 | 370 | for z <- [z_1, z_2, z_3] do 371 | assert Zipper.first_ancestral_pibling(z, predicate) == nil 372 | end 373 | end 374 | 375 | test "should return the first previous ancestral pibling found", %{ 376 | z_with_grandpiblings: z 377 | } do 378 | actual = Zipper.first_ancestral_pibling(z) 379 | assert 2 == actual.focus.term 380 | end 381 | 382 | test "should return the first previous pibling matching the predicate", %{ 383 | z_with_grandpiblings: z 384 | } do 385 | predicate = &(&1.term == 3) 386 | 387 | actual = Zipper.first_ancestral_pibling(z, predicate) 388 | assert 3 == actual.focus.term 389 | end 390 | end 391 | 392 | describe "last_ancestral_pibling/2" do 393 | test "should return nil if no parent found", %{simple_z: z} do 394 | assert Zipper.last_ancestral_pibling(z) == nil 395 | end 396 | 397 | test "should return nil if no ancestors have siblings", %{z_with_no_ancestral_piblings: z} do 398 | assert Zipper.last_ancestral_pibling(z) == nil 399 | end 400 | 401 | test "should return nil if no next pibling for any ancestor found matching the predicate", 402 | %{ 403 | z_with_piblings: z_1, 404 | z_with_grandpiblings: z_2, 405 | z_with_ancestral_piblings: z_3 406 | } do 407 | predicate = &(&1.term == :not_found) 408 | 409 | for z <- [z_1, z_2, z_3] do 410 | assert Zipper.last_ancestral_pibling(z, predicate) == nil 411 | end 412 | end 413 | 414 | test "should return the first next ancestral pibling found", %{ 415 | z_with_grandpiblings: z 416 | } do 417 | actual = Zipper.last_ancestral_pibling(z) 418 | assert 8 == actual.focus.term 419 | end 420 | 421 | test "should return the first next ancestral pibling matching the predicate", %{ 422 | z_with_grandpiblings: z 423 | } do 424 | predicate = &(&1.term == 7) 425 | 426 | actual = Zipper.last_ancestral_pibling(z, predicate) 427 | assert 7 == actual.focus.term 428 | end 429 | end 430 | 431 | describe "previous_ancestral_pibling/2" do 432 | test "should return nil if no parent found", %{simple_z: z} do 433 | assert Zipper.previous_ancestral_pibling(z) == nil 434 | end 435 | 436 | test "should return nil if no ancestors have siblings", %{z_with_no_ancestral_piblings: z} do 437 | assert Zipper.previous_ancestral_pibling(z) == nil 438 | end 439 | 440 | test "should return nil if no previous pibling for any ancestor found matching the predicate", 441 | %{ 442 | z_with_piblings: z_1, 443 | z_with_grandpiblings: z_2, 444 | z_with_ancestral_piblings: z_3 445 | } do 446 | predicate = &(&1.term == :not_found) 447 | 448 | for z <- [z_1, z_2, z_3] do 449 | assert Zipper.previous_ancestral_pibling(z, predicate) == nil 450 | end 451 | end 452 | 453 | test "should return the first previous ancestral pibling found", %{ 454 | z_with_grandpiblings: z 455 | } do 456 | actual = Zipper.previous_ancestral_pibling(z) 457 | assert 4 == actual.focus.term 458 | end 459 | 460 | test "should return the first previous pibling matching the predicate", %{ 461 | z_with_grandpiblings: z 462 | } do 463 | predicate = &(&1.term == 3) 464 | 465 | actual = Zipper.previous_ancestral_pibling(z, predicate) 466 | assert 3 == actual.focus.term 467 | end 468 | end 469 | 470 | describe "next_ancestral_pibling/2" do 471 | test "should return nil if no parent found", %{simple_z: z} do 472 | assert Zipper.next_ancestral_pibling(z) == nil 473 | end 474 | 475 | test "should return nil if no ancestors have siblings", %{z_with_no_ancestral_piblings: z} do 476 | assert Zipper.next_ancestral_pibling(z) == nil 477 | end 478 | 479 | test "should return nil if no next pibling for any ancestor found matching the predicate", 480 | %{ 481 | z_with_piblings: z_1, 482 | z_with_grandpiblings: z_2, 483 | z_with_ancestral_piblings: z_3 484 | } do 485 | predicate = &(&1.term == :not_found) 486 | 487 | for z <- [z_1, z_2, z_3] do 488 | assert Zipper.next_ancestral_pibling(z, predicate) == nil 489 | end 490 | end 491 | 492 | test "should return the first next ancestral pibling found", %{ 493 | z_with_grandpiblings: z 494 | } do 495 | actual = Zipper.next_ancestral_pibling(z) 496 | assert 6 == actual.focus.term 497 | end 498 | 499 | test "should return the first next ancestral pibling matching the predicate", %{ 500 | z_with_grandpiblings: z 501 | } do 502 | predicate = &(&1.term == 7) 503 | 504 | actual = Zipper.next_ancestral_pibling(z, predicate) 505 | assert 7 == actual.focus.term 506 | end 507 | end 508 | 509 | describe "first_extended_pibling/2" do 510 | test "should return nil if no parent found", %{simple_z: z} do 511 | assert Zipper.first_extended_pibling(z) == nil 512 | end 513 | 514 | test "should return nil if no grandparent found", %{z_with_parent: z} do 515 | assert Zipper.first_extended_pibling(z) == nil 516 | end 517 | 518 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 519 | assert Zipper.first_extended_pibling(z) == nil 520 | end 521 | 522 | test "should return nil if no previous grandpibling has children", 523 | %{z_with_grandpiblings: z} do 524 | assert Zipper.first_extended_pibling(z) == nil 525 | end 526 | 527 | test "should return nil if no extended pibling found matching predicate", 528 | %{ 529 | z_with_extended_cousins: z 530 | } do 531 | predicate = &(&1.term == :not_found) 532 | 533 | assert Zipper.first_extended_pibling(z, predicate) == nil 534 | end 535 | 536 | test "should return the first extended pibling found", 537 | %{z_with_extended_cousins: z} do 538 | assert %Zipper{focus: actual} = Zipper.first_extended_pibling(z) 539 | assert 50 == actual.term 540 | end 541 | 542 | test "should return the first extended pibling found matching the predicate", 543 | %{z_with_extended_cousins: z} do 544 | predicate = &(&1.term == 45) 545 | 546 | assert %Zipper{focus: actual} = Zipper.first_extended_pibling(z, predicate) 547 | assert 45 == actual.term 548 | end 549 | end 550 | 551 | describe "last_extended_pibling/2" do 552 | test "should return nil if no parent found", %{simple_z: z} do 553 | assert Zipper.last_extended_pibling(z) == nil 554 | end 555 | 556 | test "should return nil if no grandparent found", %{z_with_parent: z} do 557 | assert Zipper.last_extended_pibling(z) == nil 558 | end 559 | 560 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 561 | assert Zipper.last_extended_pibling(z) == nil 562 | end 563 | 564 | test "should return nil if no next grandpibling has children", 565 | %{z_with_grandpiblings: z} do 566 | assert Zipper.last_extended_pibling(z) == nil 567 | end 568 | 569 | test "should return nil if no extended pibling found matching predicate", 570 | %{ 571 | z_with_extended_cousins: z 572 | } do 573 | predicate = &(&1.term == :not_found) 574 | 575 | assert Zipper.last_extended_pibling(z, predicate) == nil 576 | end 577 | 578 | test "should return the last extended pibling found", 579 | %{z_with_extended_cousins: z} do 580 | assert %Zipper{focus: actual} = Zipper.last_extended_pibling(z) 581 | assert 58 == actual.term 582 | end 583 | 584 | test "should return the last extended pibling found matching the predicate", 585 | %{z_with_extended_cousins: z} do 586 | predicate = &(&1.term == 54) 587 | 588 | assert %Zipper{focus: actual} = Zipper.last_extended_pibling(z, predicate) 589 | assert 54 == actual.term 590 | end 591 | end 592 | 593 | describe "previous_extended_pibling/2" do 594 | test "should return nil if no parent found", %{simple_z: z} do 595 | assert Zipper.previous_extended_pibling(z) == nil 596 | end 597 | 598 | test "should return nil if no grandparent found", %{z_with_parent: z} do 599 | assert Zipper.previous_extended_pibling(z) == nil 600 | end 601 | 602 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 603 | assert Zipper.previous_extended_pibling(z) == nil 604 | end 605 | 606 | test "should return nil if no previous grandpibling has children", 607 | %{z_with_grandpiblings: z} do 608 | assert Zipper.previous_extended_pibling(z) == nil 609 | end 610 | 611 | test "should return nil if no extended pibling found matching predicate", 612 | %{ 613 | z_with_extended_cousins: z 614 | } do 615 | predicate = &(&1.term == :not_found) 616 | 617 | assert Zipper.previous_extended_pibling(z, predicate) == nil 618 | end 619 | 620 | test "should return the previous extended pibling found", 621 | %{z_with_extended_cousins: z} do 622 | assert %Zipper{focus: actual} = Zipper.previous_extended_pibling(z) 623 | assert 49 == actual.term 624 | end 625 | 626 | test "should return the previous extended pibling found matching the predicate", 627 | %{z_with_extended_cousins: z} do 628 | predicate = &(&1.term == 45) 629 | 630 | assert %Zipper{focus: actual} = Zipper.previous_extended_pibling(z, predicate) 631 | assert 45 == actual.term 632 | end 633 | end 634 | 635 | describe "next_extended_pibling/2" do 636 | test "should return nil if no parent found", %{simple_z: z} do 637 | assert Zipper.next_extended_pibling(z) == nil 638 | end 639 | 640 | test "should return nil if no grandparent found", %{z_with_parent: z} do 641 | assert Zipper.next_extended_pibling(z) == nil 642 | end 643 | 644 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 645 | assert Zipper.next_extended_pibling(z) == nil 646 | end 647 | 648 | test "should return nil if no next grandpibling has children", 649 | %{z_with_grandpiblings: z} do 650 | assert Zipper.next_extended_pibling(z) == nil 651 | end 652 | 653 | test "should return nil if no extended pibling found matching predicate", 654 | %{ 655 | z_with_extended_cousins: z 656 | } do 657 | predicate = &(&1.term == :not_found) 658 | 659 | assert Zipper.next_extended_pibling(z, predicate) == nil 660 | end 661 | 662 | test "should return the next extended pibling found", 663 | %{z_with_extended_cousins: z} do 664 | assert %Zipper{focus: actual} = Zipper.next_extended_pibling(z) 665 | assert 52 == actual.term 666 | end 667 | 668 | test "should return the next extended pibling found matching the predicate", 669 | %{z_with_extended_cousins: z} do 670 | predicate = &(&1.term == 54) 671 | 672 | assert %Zipper{focus: actual} = Zipper.next_extended_pibling(z, predicate) 673 | assert 54 == actual.term 674 | end 675 | end 676 | end 677 | -------------------------------------------------------------------------------- /test/ex_rose_tree/zipper/second_cousin_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Zipper.SecondCousinTest do 2 | use ExUnit.Case, async: true 3 | use ZipperCase 4 | 5 | describe "first_second_cousin/2" do 6 | test "should return nil if no parent found", %{simple_z: z} do 7 | assert Zipper.first_second_cousin(z) == nil 8 | end 9 | 10 | test "should return nil if no grandparent found", %{z_with_parent: z} do 11 | assert Zipper.first_second_cousin(z) == nil 12 | end 13 | 14 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 15 | assert Zipper.first_second_cousin(z) == nil 16 | end 17 | 18 | test "should return nil if no previous grandpibling has children", 19 | %{z_with_grandpiblings: z} do 20 | assert Zipper.first_second_cousin(z) == nil 21 | end 22 | 23 | test "should return nil if no second-cousin found matching predicate", 24 | %{z_with_2nd_cousins: z} do 25 | predicate = &(&1.term == :not_found) 26 | 27 | assert Zipper.first_second_cousin(z, predicate) == nil 28 | end 29 | 30 | test "should return the first second-cousin found", %{ 31 | z_with_2nd_cousins: z 32 | } do 33 | actual = Zipper.first_second_cousin(z) 34 | assert 50 == actual.focus.term 35 | end 36 | 37 | test "should return the first second-cousin matching the predicate", %{ 38 | z_with_2nd_cousins: z 39 | } do 40 | predicate = &(&1.term == 45) 41 | 42 | actual = Zipper.first_second_cousin(z, predicate) 43 | assert 45 == actual.focus.term 44 | end 45 | 46 | test "should return nil and not seek past the original grandparent for a predicate match", %{ 47 | z_with_2nd_cousins: z 48 | } do 49 | predicate = &(&1.term == 58) 50 | 51 | assert Zipper.first_second_cousin(z, predicate) == nil 52 | end 53 | end 54 | 55 | describe "last_second_cousin/2" do 56 | test "should return nil if no parent found", %{simple_z: z} do 57 | assert Zipper.last_second_cousin(z) == nil 58 | end 59 | 60 | test "should return nil if no grandparent found", %{z_with_parent: z} do 61 | assert Zipper.last_second_cousin(z) == nil 62 | end 63 | 64 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 65 | assert Zipper.last_second_cousin(z) == nil 66 | end 67 | 68 | test "should return nil if no next grandpibling has children", 69 | %{z_with_grandpiblings: z} do 70 | assert Zipper.last_second_cousin(z) == nil 71 | end 72 | 73 | test "should return nil if no second-cousin found matching predicate", 74 | %{z_with_2nd_cousins: z} do 75 | predicate = &(&1.term == :not_found) 76 | 77 | assert Zipper.last_second_cousin(z, predicate) == nil 78 | end 79 | 80 | test "should return the last second-cousin found", %{ 81 | z_with_2nd_cousins: z 82 | } do 83 | actual = Zipper.last_second_cousin(z) 84 | assert 58 == actual.focus.term 85 | end 86 | 87 | test "should return the last second-cousin matching the predicate", %{ 88 | z_with_2nd_cousins: z 89 | } do 90 | predicate = &(&1.term == 55) 91 | 92 | actual = Zipper.last_second_cousin(z, predicate) 93 | assert 55 == actual.focus.term 94 | end 95 | 96 | test "should return nil and not seek before the original grandparent for a predicate match", 97 | %{ 98 | z_with_2nd_cousins: z 99 | } do 100 | predicate = &(&1.term == 45) 101 | 102 | assert Zipper.last_second_cousin(z, predicate) == nil 103 | end 104 | end 105 | 106 | describe "previous_second_cousin/2" do 107 | test "should return nil if no parent found", %{simple_z: z} do 108 | assert Zipper.previous_second_cousin(z) == nil 109 | end 110 | 111 | test "should return nil if no grandparent found", %{z_with_parent: z} do 112 | assert Zipper.previous_second_cousin(z) == nil 113 | end 114 | 115 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 116 | assert Zipper.previous_second_cousin(z) == nil 117 | end 118 | 119 | test "should return nil if no previous grandpibling has children", 120 | %{z_with_grandpiblings: z} do 121 | assert Zipper.previous_second_cousin(z) == nil 122 | end 123 | 124 | test "should return nil if no second-cousin found matching predicate", 125 | %{z_with_2nd_cousins: z} do 126 | predicate = &(&1.term == :not_found) 127 | 128 | assert Zipper.previous_second_cousin(z, predicate) == nil 129 | end 130 | 131 | test "should return the first previous second-cousin found", %{ 132 | z_with_2nd_cousins: z 133 | } do 134 | actual = Zipper.previous_second_cousin(z) 135 | assert 49 == actual.focus.term 136 | end 137 | 138 | test "should return the first previous second-cousin matching the predicate", %{ 139 | z_with_2nd_cousins: z 140 | } do 141 | predicate = &(&1.term == 49) 142 | 143 | actual = Zipper.previous_second_cousin(z, predicate) 144 | assert 49 == actual.focus.term 145 | end 146 | 147 | test "should return nil and not seek past the original grandparent for a predicate match", %{ 148 | z_with_2nd_cousins: z 149 | } do 150 | predicate = &(&1.term == 54) 151 | 152 | assert Zipper.previous_second_cousin(z, predicate) == nil 153 | end 154 | end 155 | 156 | describe "next_second_cousin/2" do 157 | test "should return nil if no parent found", %{simple_z: z} do 158 | assert Zipper.next_second_cousin(z) == nil 159 | end 160 | 161 | test "should return nil if no grandparent found", %{z_with_parent: z} do 162 | assert Zipper.next_second_cousin(z) == nil 163 | end 164 | 165 | test "should return nil if grandparent has no siblings", %{z_with_grandparent: z} do 166 | assert Zipper.next_second_cousin(z) == nil 167 | end 168 | 169 | test "should return nil if no next grandpibling has children", 170 | %{z_with_grandpiblings: z} do 171 | assert Zipper.next_second_cousin(z) == nil 172 | end 173 | 174 | test "should return nil if no second-cousin found matching predicate", 175 | %{z_with_2nd_cousins: z} do 176 | predicate = &(&1.term == :not_found) 177 | 178 | assert Zipper.next_second_cousin(z, predicate) == nil 179 | end 180 | 181 | test "should return the next second-cousin found", %{ 182 | z_with_2nd_cousins: z 183 | } do 184 | actual = Zipper.next_second_cousin(z) 185 | assert 52 == actual.focus.term 186 | end 187 | 188 | test "should return the next second-cousin matching the predicate", %{ 189 | z_with_2nd_cousins: z 190 | } do 191 | predicate = &(&1.term == 55) 192 | 193 | actual = Zipper.next_second_cousin(z, predicate) 194 | assert 55 == actual.focus.term 195 | end 196 | 197 | test "should return nil and not seek before the original grandparent for a predicate match", 198 | %{ 199 | z_with_2nd_cousins: z 200 | } do 201 | predicate = &(&1.term == 45) 202 | 203 | assert Zipper.next_second_cousin(z, predicate) == nil 204 | end 205 | end 206 | end 207 | -------------------------------------------------------------------------------- /test/ex_rose_tree/zipper/sibling_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.Zipper.Zipper.SiblingTest do 2 | use ExUnit.Case, async: true 3 | use ZipperCase 4 | 5 | describe "prepend_first_sibling/2" do 6 | test "should increase the number of previous siblings by 1", %{z_with_siblings: z} do 7 | new_term = :anything 8 | new_tree = ExRoseTree.new(new_term) 9 | 10 | for _ <- [new_term, new_tree] do 11 | assert %Zipper{prev: actual} = Zipper.prepend_first_sibling(z, new_tree) 12 | assert Enum.count(actual) == Enum.count(z.prev) + 1 13 | end 14 | end 15 | 16 | test "should add the new ExRoseTree to the end of the previous siblings since they are reversed", 17 | %{z_with_siblings: z} do 18 | new_tree = ExRoseTree.new(:anything) 19 | 20 | assert %Zipper{prev: actual} = Zipper.prepend_first_sibling(z, new_tree) 21 | assert [^new_tree | _] = Enum.reverse(actual) 22 | end 23 | 24 | test "should add the term as a new ExRoseTree to the end of the previous siblings since they are reversed", 25 | %{z_with_siblings: z} do 26 | new_term = :anything 27 | 28 | expected_tree = ExRoseTree.new(new_term) 29 | 30 | assert %Zipper{prev: actual} = Zipper.prepend_first_sibling(z, new_term) 31 | assert [^expected_tree | _] = Enum.reverse(actual) 32 | end 33 | end 34 | 35 | describe "append_last_sibling/2" do 36 | test "should increase the number of next siblings by 1", %{z_with_siblings: z} do 37 | new_term = :anything 38 | new_tree = ExRoseTree.new(new_term) 39 | 40 | for _ <- [new_term, new_tree] do 41 | assert %Zipper{next: actual} = Zipper.append_last_sibling(z, new_tree) 42 | assert Enum.count(actual) == Enum.count(z.next) + 1 43 | end 44 | end 45 | 46 | test "should add the new ExRoseTree to the end of the next siblings", %{z_with_siblings: z} do 47 | new_tree = ExRoseTree.new(:anything) 48 | 49 | assert %Zipper{next: actual} = Zipper.append_last_sibling(z, new_tree) 50 | assert [^new_tree | _] = Enum.reverse(actual) 51 | end 52 | 53 | test "should add the term as a new ExRoseTree to the end of the next siblings", %{ 54 | z_with_siblings: z 55 | } do 56 | new_term = :anything 57 | 58 | expected_tree = ExRoseTree.new(new_term) 59 | 60 | assert %Zipper{next: actual} = Zipper.append_last_sibling(z, new_term) 61 | assert [^expected_tree | _] = Enum.reverse(actual) 62 | end 63 | end 64 | 65 | describe "append_previous_sibling/2" do 66 | test "should increase the number of previous siblings by 1", %{z_with_siblings: z} do 67 | new_term = :anything 68 | new_tree = ExRoseTree.new(new_term) 69 | 70 | for _ <- [new_term, new_tree] do 71 | assert %Zipper{prev: actual} = Zipper.append_previous_sibling(z, new_tree) 72 | assert Enum.count(actual) == Enum.count(z.prev) + 1 73 | end 74 | end 75 | 76 | test "should add the new ExRoseTree to the head of the previous siblings since they are reversed", 77 | %{z_with_siblings: z} do 78 | new_tree = ExRoseTree.new(:anything) 79 | 80 | assert %Zipper{prev: actual} = Zipper.append_previous_sibling(z, new_tree) 81 | assert [^new_tree | _] = actual 82 | end 83 | 84 | test "should add the term as a new ExRoseTree to the head of the previous siblings since they are reversed", 85 | %{z_with_siblings: z} do 86 | new_term = :anything 87 | 88 | expected_tree = ExRoseTree.new(new_term) 89 | 90 | assert %Zipper{prev: actual} = Zipper.append_previous_sibling(z, new_term) 91 | assert [^expected_tree | _] = actual 92 | end 93 | end 94 | 95 | describe "prepend_next_sibling/2" do 96 | test "should increase the number of next siblings by 1", %{z_with_siblings: z} do 97 | new_term = :anything 98 | new_tree = ExRoseTree.new(new_term) 99 | 100 | for _ <- [new_term, new_tree] do 101 | assert %Zipper{next: actual} = Zipper.prepend_next_sibling(z, new_tree) 102 | assert Enum.count(actual) == Enum.count(z.next) + 1 103 | end 104 | end 105 | 106 | test "should add the new ExRoseTree to the head of the next siblings", %{z_with_siblings: z} do 107 | new_tree = ExRoseTree.new(:anything) 108 | 109 | assert %Zipper{next: actual} = Zipper.prepend_next_sibling(z, new_tree) 110 | assert [^new_tree | _] = actual 111 | end 112 | 113 | test "should add the term as a new ExRoseTree to the next of the next siblings", %{ 114 | z_with_siblings: z 115 | } do 116 | new_term = :anything 117 | 118 | expected_tree = ExRoseTree.new(new_term) 119 | 120 | assert %Zipper{next: actual} = Zipper.prepend_next_sibling(z, new_term) 121 | assert [^expected_tree | _] = actual 122 | end 123 | end 124 | 125 | describe "insert_previous_sibling_at/3" do 126 | test "should increase the number of previous siblings by 1", %{z_with_siblings: z} do 127 | new_term = :anything 128 | new_tree = ExRoseTree.new(new_term) 129 | 130 | for _ <- [new_term, new_tree] do 131 | assert %Zipper{prev: actual} = Zipper.insert_previous_sibling_at(z, new_tree, 0) 132 | assert Enum.count(actual) == Enum.count(z.prev) + 1 133 | end 134 | end 135 | 136 | test "should insert a new ExRoseTree to previous siblings", %{z_with_siblings: z} do 137 | new_tree = ExRoseTree.new(:anything) 138 | 139 | assert %Zipper{prev: actual} = Zipper.insert_previous_sibling_at(z, new_tree, 0) 140 | assert [^new_tree | _] = Enum.reverse(actual) 141 | end 142 | 143 | test "should insert a term as a new ExRoseTree to previous siblings", %{z_with_siblings: z} do 144 | new_term = :anything 145 | 146 | expected_tree = ExRoseTree.new(new_term) 147 | 148 | assert %Zipper{prev: actual} = Zipper.insert_previous_sibling_at(z, new_term, 0) 149 | assert [^expected_tree | _] = Enum.reverse(actual) 150 | end 151 | 152 | test "should insert a new ExRoseTree at the correct index, starting from the back since its reversed, when given a positive index", 153 | %{z_with_siblings: z} do 154 | new_tree = ExRoseTree.new(:anything) 155 | 156 | assert %Zipper{prev: actual} = Zipper.insert_previous_sibling_at(z, new_tree, 3) 157 | assert new_tree == Enum.at(Enum.reverse(actual), 3) 158 | end 159 | 160 | test "should insert a new ExRoseTree at the head when given a positive index greater than count of previous siblings since they are reversed", 161 | %{z_with_siblings: z} do 162 | new_tree = ExRoseTree.new(:anything) 163 | 164 | assert %Zipper{prev: actual} = Zipper.insert_previous_sibling_at(z, new_tree, 10) 165 | assert [^new_tree | _] = actual 166 | end 167 | 168 | test "should insert a new ExRoseTree at the correct index, starting from from the front esince its reversed, when given a negative index", 169 | %{z_with_siblings: z} do 170 | new_tree = ExRoseTree.new(:anything) 171 | 172 | assert %Zipper{prev: actual} = Zipper.insert_previous_sibling_at(z, new_tree, -2) 173 | assert new_tree == Enum.at(actual, 2) 174 | end 175 | 176 | test "should insert a new ExRoseTree at the end when given a negatve index greater than count of previous siblings since they are reversed", 177 | %{z_with_siblings: z} do 178 | new_tree = ExRoseTree.new(:anything) 179 | 180 | assert %Zipper{prev: actual} = Zipper.insert_previous_sibling_at(z, new_tree, -10) 181 | assert [^new_tree | _] = Enum.reverse(actual) 182 | end 183 | end 184 | 185 | describe "insert_next_sibling_at/3" do 186 | test "should increase the number of next siblings by 1", %{z_with_siblings: z} do 187 | new_term = :anything 188 | new_tree = ExRoseTree.new(new_term) 189 | 190 | for _ <- [new_term, new_tree] do 191 | assert %Zipper{next: actual} = Zipper.insert_next_sibling_at(z, new_tree, 0) 192 | assert Enum.count(actual) == Enum.count(z.prev) + 1 193 | end 194 | end 195 | 196 | test "should insert a new ExRoseTree to next siblings", %{z_with_siblings: z} do 197 | new_tree = ExRoseTree.new(:anything) 198 | 199 | assert %Zipper{next: [^new_tree | _]} = Zipper.insert_next_sibling_at(z, new_tree, 0) 200 | end 201 | 202 | test "should insert a term as a new ExRoseTree to next siblings", %{z_with_siblings: z} do 203 | new_term = :anything 204 | 205 | expected_tree = ExRoseTree.new(new_term) 206 | 207 | assert %Zipper{next: [^expected_tree | _]} = Zipper.insert_next_sibling_at(z, new_term, 0) 208 | end 209 | 210 | test "should insert a new ExRoseTree at the correct index when given a positive index", %{ 211 | z_with_siblings: z 212 | } do 213 | new_tree = ExRoseTree.new(:anything) 214 | 215 | assert %Zipper{next: actual} = Zipper.insert_next_sibling_at(z, new_tree, 3) 216 | assert new_tree == Enum.at(actual, 3) 217 | end 218 | 219 | test "should insert a new ExRoseTree at the end when given a positive index greater than count of next siblings", 220 | %{z_with_siblings: z} do 221 | new_tree = ExRoseTree.new(:anything) 222 | 223 | assert %Zipper{next: actual} = Zipper.insert_next_sibling_at(z, new_tree, 10) 224 | assert [^new_tree | _] = Enum.reverse(actual) 225 | end 226 | 227 | test "should insert a new ExRoseTree at the correct index, starting from the back, when given a negative index", 228 | %{z_with_siblings: z} do 229 | new_tree = ExRoseTree.new(:anything) 230 | 231 | assert %Zipper{next: actual} = Zipper.insert_next_sibling_at(z, new_tree, -2) 232 | assert new_tree == Enum.at(actual, 2) 233 | end 234 | 235 | test "should insert a new ExRoseTree at the head when given a negative index greater than count of next siblings", 236 | %{z_with_siblings: z} do 237 | new_tree = ExRoseTree.new(:anything) 238 | 239 | assert %Zipper{next: [^new_tree | _]} = Zipper.insert_next_sibling_at(z, new_tree, -10) 240 | end 241 | end 242 | 243 | describe "pop_first_sibling/1" do 244 | test "should return unchanged Zipper and nil when no previous siblings exist", %{simple_z: z} do 245 | assert {^z, nil} = Zipper.pop_first_sibling(z) 246 | end 247 | 248 | test "should return Zipper with one less previous sibling", %{z_with_siblings: z} do 249 | assert {%Zipper{prev: actual}, %ExRoseTree{} = _removed} = Zipper.pop_first_sibling(z) 250 | assert Enum.count(actual) == Enum.count(z.prev) - 1 251 | end 252 | 253 | test "should return Zipper with first sibling removed", %{z_with_siblings: z} do 254 | {expected_remainder, [expected_removal | []]} = Enum.split(z.prev, -1) 255 | 256 | assert {%Zipper{prev: ^expected_remainder}, ^expected_removal} = Zipper.pop_first_sibling(z) 257 | end 258 | end 259 | 260 | describe "pop_previous_sibling/1" do 261 | test "should return unchanged Zipper and nil when no previous siblings exist", %{simple_z: z} do 262 | assert {^z, nil} = Zipper.pop_previous_sibling(z) 263 | end 264 | 265 | test "should return Zipper with one less previous sibling", %{z_with_siblings: z} do 266 | assert {%Zipper{prev: actual}, %ExRoseTree{} = _removed} = Zipper.pop_previous_sibling(z) 267 | assert Enum.count(actual) == Enum.count(z.prev) - 1 268 | end 269 | 270 | test "should return Zipper with previous sibling removed", %{z_with_siblings: z} do 271 | [expected_removal | expected_remainder] = z.prev 272 | 273 | assert {%Zipper{prev: ^expected_remainder}, ^expected_removal} = 274 | Zipper.pop_previous_sibling(z) 275 | end 276 | end 277 | 278 | describe "pop_last_sibling/1" do 279 | test "should return unchanged Zipper and nil when no next siblings exist", %{simple_z: z} do 280 | assert {^z, nil} = Zipper.pop_last_sibling(z) 281 | end 282 | 283 | test "should return Zipper with one less next sibling", %{z_with_siblings: z} do 284 | assert {%Zipper{next: actual}, %ExRoseTree{} = _removed} = Zipper.pop_last_sibling(z) 285 | assert Enum.count(actual) == Enum.count(z.next) - 1 286 | end 287 | 288 | test "should return Zipper with last sibling removed", %{z_with_siblings: z} do 289 | {expected_remainder, [expected_removal | []]} = Enum.split(z.next, -1) 290 | 291 | assert {%Zipper{next: ^expected_remainder}, ^expected_removal} = Zipper.pop_last_sibling(z) 292 | end 293 | end 294 | 295 | describe "pop_next_sibling/1" do 296 | test "should return unchanged Zipper and nil when no next siblings exist", %{simple_z: z} do 297 | assert {^z, nil} = Zipper.pop_next_sibling(z) 298 | end 299 | 300 | test "should return Zipper with one less next sibling", %{z_with_siblings: z} do 301 | assert {%Zipper{next: actual}, %ExRoseTree{} = _removed} = Zipper.pop_next_sibling(z) 302 | assert Enum.count(actual) == Enum.count(z.next) - 1 303 | end 304 | 305 | test "should return Zipper with next sibling removed", %{z_with_siblings: z} do 306 | [expected_removal | expected_remainder] = z.next 307 | 308 | assert {%Zipper{next: ^expected_remainder}, ^expected_removal} = Zipper.pop_next_sibling(z) 309 | end 310 | end 311 | 312 | describe "pop_previous_sibling_at/3" do 313 | test "should return unchanged Zipper and nil when no previous siblings exist", %{simple_z: z} do 314 | assert {^z, nil} = Zipper.pop_previous_sibling_at(z, 0) 315 | end 316 | 317 | test "should decrease the number of previous siblings by 1", %{z_with_siblings: z} do 318 | assert {%Zipper{prev: actual}, %ExRoseTree{} = _removed} = 319 | Zipper.pop_previous_sibling_at(z, 0) 320 | 321 | assert Enum.count(actual) == Enum.count(z.prev) - 1 322 | end 323 | 324 | test "should remove the sibling at the correct index, starting from the back since its reversed, when given a positive index", 325 | %{z_with_siblings: z} do 326 | assert {%Zipper{prev: actual}, removed} = Zipper.pop_previous_sibling_at(z, 1) 327 | assert removed == Enum.at(z.prev, 2) 328 | assert [4, 3, 1] = Enum.map(actual, & &1.term) 329 | end 330 | 331 | test "should not remove any sibling when given a positive index greater than count of previous siblings", 332 | %{z_with_siblings: z} do 333 | assert {^z, nil} = Zipper.pop_previous_sibling_at(z, 10) 334 | end 335 | 336 | test "should remove the sibling at the correct index, starting from from the front since its reversed, when given a negative index", 337 | %{z_with_siblings: z} do 338 | assert {%Zipper{prev: actual}, removed} = Zipper.pop_previous_sibling_at(z, -3) 339 | assert removed == Enum.at(z.prev, 2) 340 | assert [4, 3, 1] = Enum.map(actual, & &1.term) 341 | end 342 | 343 | test "should not remove any sibling when given a negative index greater than count of previous siblings", 344 | %{z_with_siblings: z} do 345 | assert {^z, nil} = Zipper.pop_previous_sibling_at(z, -10) 346 | end 347 | end 348 | 349 | describe "pop_next_sibling_at/3" do 350 | test "should return unchanged Zipper and nil when no next siblings exist", %{simple_z: z} do 351 | assert {^z, nil} = Zipper.pop_next_sibling_at(z, 0) 352 | end 353 | 354 | test "should decrease the number of next siblings by 1", %{z_with_siblings: z} do 355 | assert {%Zipper{next: actual}, %ExRoseTree{} = _removed} = Zipper.pop_next_sibling_at(z, 0) 356 | assert Enum.count(actual) == Enum.count(z.next) - 1 357 | end 358 | 359 | test "should remove the sibling at the correct index when given a positive index", %{ 360 | z_with_siblings: z 361 | } do 362 | assert {%Zipper{next: actual}, removed} = Zipper.pop_next_sibling_at(z, 1) 363 | assert removed == Enum.at(z.next, 1) 364 | assert [6, 8, 9] = Enum.map(actual, & &1.term) 365 | end 366 | 367 | test "should not remove any sibling when given a positive index greater than count of next siblings", 368 | %{z_with_siblings: z} do 369 | assert {^z, nil} = Zipper.pop_next_sibling_at(z, 10) 370 | end 371 | 372 | test "should remove the sibling at the correct index, starting from from the back, when given a negative index", 373 | %{z_with_siblings: z} do 374 | assert {%Zipper{next: actual}, removed} = Zipper.pop_next_sibling_at(z, -3) 375 | assert removed == Enum.at(z.next, 1) 376 | assert [6, 8, 9] = Enum.map(actual, & &1.term) 377 | end 378 | 379 | test "should not remove any sibling when given a negative index greater than count of next siblings", 380 | %{z_with_siblings: z} do 381 | assert {^z, nil} = Zipper.pop_next_sibling_at(z, -10) 382 | end 383 | end 384 | 385 | describe "first_sibling/2" do 386 | test "should return nil if Zipper has no previous siblings", %{simple_z: z} do 387 | assert Zipper.first_sibling(z) == nil 388 | end 389 | 390 | test "should return nil if no previous sibling is found for Zipper that matches the predicate", 391 | %{z_with_siblings: z} do 392 | predicate = &(&1.term == :not_found) 393 | 394 | assert Zipper.first_sibling(z, predicate) == nil 395 | end 396 | 397 | test "should return the first sibling node for Zipper", %{ 398 | z_with_siblings: z 399 | } do 400 | actual = Zipper.first_sibling(z) 401 | assert 1 == actual.focus.term 402 | end 403 | 404 | test "should return the first sibling node for Zipper that matches the predicate", %{ 405 | z_with_siblings: z 406 | } do 407 | predicate = &(&1.term == 3) 408 | 409 | actual = Zipper.first_sibling(z, predicate) 410 | assert 3 == actual.focus.term 411 | end 412 | 413 | test "should return nil and not seek past the original Zipper for a predicate match", %{ 414 | z_with_siblings: z 415 | } do 416 | predicate = &(&1.term == 7) 417 | 418 | assert Zipper.first_sibling(z, predicate) == nil 419 | end 420 | end 421 | 422 | describe "previous_sibling/2" do 423 | test "should return nil if Zipper has no previous siblings", %{simple_z: z} do 424 | assert Zipper.previous_sibling(z) == nil 425 | end 426 | 427 | test "should return nil if no previous sibling is found for Zipper that matches the predicate", 428 | %{z_with_siblings: z} do 429 | predicate = &(&1.term == :not_found) 430 | 431 | assert Zipper.previous_sibling(z, predicate) == nil 432 | end 433 | 434 | test "should return the previous sibling node for Zipper", %{ 435 | z_with_siblings: z 436 | } do 437 | actual = Zipper.previous_sibling(z) 438 | assert 4 == actual.focus.term 439 | end 440 | 441 | test "should return the first previous sibling node for Zipper that matches the predicate", 442 | %{ 443 | z_with_siblings: z 444 | } do 445 | predicate = &(&1.term == 2) 446 | 447 | actual = Zipper.previous_sibling(z, predicate) 448 | assert 2 == actual.focus.term 449 | end 450 | end 451 | 452 | describe "last_sibling/2" do 453 | test "should return nil if Zipper has no next siblings", %{simple_z: z} do 454 | assert Zipper.last_sibling(z) == nil 455 | end 456 | 457 | test "should return nil if no next sibling is found for Zipper that matches the predicate", 458 | %{z_with_siblings: z} do 459 | predicate = &(&1.term == :not_found) 460 | 461 | assert Zipper.last_sibling(z, predicate) == nil 462 | end 463 | 464 | test "should return the last sibling node for Zipper", %{ 465 | z_with_siblings: z 466 | } do 467 | actual = Zipper.last_sibling(z) 468 | assert 9 == actual.focus.term 469 | end 470 | 471 | test "should return the last next sibling node for Zipper that matches the predicate", %{ 472 | z_with_siblings: z 473 | } do 474 | predicate = &(&1.term == 7) 475 | 476 | actual = Zipper.last_sibling(z, predicate) 477 | assert 7 == actual.focus.term 478 | end 479 | 480 | test "should return nil and not seek before the original Zipper for a predicate match", %{ 481 | z_with_siblings: z 482 | } do 483 | predicate = &(&1.term == 3) 484 | 485 | assert Zipper.last_sibling(z, predicate) == nil 486 | end 487 | end 488 | 489 | describe "next_sibling/2" do 490 | test "should return nil if Zipper has no next siblings", %{simple_z: z} do 491 | assert Zipper.next_sibling(z) == nil 492 | end 493 | 494 | test "should return nil if no next sibling is found for Zipper that matches the predicate", 495 | %{z_with_siblings: z} do 496 | predicate = &(&1.term == :not_found) 497 | 498 | assert Zipper.next_sibling(z, predicate) == nil 499 | end 500 | 501 | test "should return the next sibling node for Zipper", %{ 502 | z_with_siblings: z 503 | } do 504 | actual = Zipper.next_sibling(z) 505 | assert 6 == actual.focus.term 506 | end 507 | 508 | test "should return the first next sibling node for Zipper that matches the predicate", 509 | %{ 510 | z_with_siblings: z 511 | } do 512 | predicate = &(&1.term == 8) 513 | 514 | actual = Zipper.next_sibling(z, predicate) 515 | assert 8 == actual.focus.term 516 | end 517 | end 518 | 519 | describe "sibling_at/2" do 520 | test "should return nil when given a Zipper with no siblings", %{simple_z: z} do 521 | for _ <- 0..5 do 522 | idx = Enum.random(0..10) 523 | assert Zipper.sibling_at(z, idx) == nil 524 | end 525 | end 526 | 527 | test "should return nil when given an index that is out of bounds for the siblings of the Zipper", 528 | %{z_with_siblings: z} do 529 | num_siblings = Enum.count(z.prev) + Enum.count(z.next) + 1 530 | 531 | for _ <- 0..5 do 532 | idx = Enum.random(num_siblings..20) 533 | assert Zipper.sibling_at(z, idx) == nil 534 | end 535 | end 536 | 537 | test "should return nil when given an index that matches the current Zipper's index", 538 | %{z_with_siblings: z} do 539 | current_idx = Enum.count(z.prev) 540 | 541 | assert Zipper.sibling_at(z, current_idx) == nil 542 | end 543 | end 544 | end 545 | -------------------------------------------------------------------------------- /test/ex_rose_tree/zipper_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.ZipperTest do 2 | use ExUnit.Case, async: true 3 | use ZipperCase 4 | 5 | doctest ExRoseTree.Zipper 6 | 7 | @bad_zippers [ 8 | {%{focus: %ExRoseTree{term: 1, children: []}, prev: [], next: [], path: []}, 0}, 9 | {true, 1}, 10 | {false, 2}, 11 | {5, 3}, 12 | {"a word", 4}, 13 | {6.4, 5}, 14 | {:a_thing, 6}, 15 | {[1, 2, 3], 7}, 16 | {{1, 2}, 8}, 17 | {%Zipper{focus: :not_a_node, prev: [], next: [], path: []}, 9}, 18 | {%Zipper{focus: %ExRoseTree{term: 1, children: []}, prev: :not_a_list, next: [], path: []}, 19 | 10}, 20 | {%Zipper{focus: %ExRoseTree{term: 1, children: []}, prev: [], next: :not_a_list, path: []}, 21 | 11}, 22 | {%Zipper{focus: %ExRoseTree{term: 1, children: []}, prev: [], next: [], path: :not_a_list}, 23 | 12}, 24 | {nil, 13} 25 | ] 26 | 27 | describe "zipper?/1 guard" do 28 | test "should return true when given a valid Zipper struct", 29 | %{empty_z: z_1, leaf_z: z_2, simple_z: z_3, z_with_extended_cousins: z_4} do 30 | all = [z_1, z_2, z_3, z_4] |> Enum.with_index() 31 | 32 | for {z, idx} <- all do 33 | assert Zipper.zipper?(z) == true, 34 | "Expected `true` for element at index #{idx}" 35 | end 36 | end 37 | 38 | test "should return false when given bad values" do 39 | for {value, idx} <- @bad_zippers do 40 | assert Zipper.zipper?(value) == false, 41 | "Expected `false` for element at index #{idx}" 42 | end 43 | end 44 | end 45 | 46 | describe "empty?/1 guard" do 47 | test "should return true when given an empty Zipper struct", %{empty_z: z} do 48 | assert Zipper.empty?(z) == true 49 | end 50 | 51 | test "should return false when given a non-empty Zipper struct", %{simple_z: z} do 52 | assert Zipper.empty?(z) == false 53 | end 54 | 55 | test "should return false when given bad values" do 56 | for {value, idx} <- @bad_zippers do 57 | assert Zipper.empty?(value) == false, 58 | "Expected `false` for element at index #{idx}" 59 | end 60 | end 61 | end 62 | 63 | describe "at_root?/1 guard" do 64 | test "should return true when given a Zipper with an empty path", %{simple_z: z} do 65 | assert Zipper.at_root?(z) == true 66 | end 67 | 68 | test "should return false when given a Zipper with a populated path", %{ 69 | z_with_parent: z 70 | } do 71 | assert Zipper.at_root?(z) == false 72 | end 73 | 74 | test "should return false when given bad values" do 75 | for {value, idx} <- @bad_zippers do 76 | assert Zipper.at_root?(value) == false, 77 | "Expected `false` for element at index #{idx}" 78 | end 79 | end 80 | end 81 | 82 | describe "has_children?/1 guard" do 83 | test "should return true when given a Zipper whose focus has children", %{simple_z: z} do 84 | assert Zipper.has_children?(z) == true 85 | end 86 | 87 | test "should return false when given a Zipper whose focus has no children", %{leaf_z: z} do 88 | assert Zipper.has_children?(z) == false 89 | end 90 | 91 | test "should return false when given bad values" do 92 | for {value, idx} <- @bad_zippers do 93 | assert Zipper.has_children?(value) == false, 94 | "Expected `false` for element at index #{idx}" 95 | end 96 | end 97 | end 98 | 99 | describe "has_siblings?/1 guard" do 100 | test "should return true when given a Zipper who has only previous ExRoseTree siblings", %{ 101 | z_with_siblings: z 102 | } do 103 | new_z = %{z | next: []} 104 | assert Zipper.has_siblings?(new_z) == true 105 | end 106 | 107 | test "should return true when given a Zipper who has only next ExRoseTree siblings", %{ 108 | z_with_siblings: z 109 | } do 110 | new_z = %{z | prev: []} 111 | assert Zipper.has_siblings?(new_z) == true 112 | end 113 | 114 | test "should return true when given a Zipper who has both previous and next ExRoseTree siblings", 115 | %{z_with_siblings: z} do 116 | assert Zipper.has_siblings?(z) == true 117 | end 118 | 119 | test "should return false when given a Zipper who has neither previous or next ExRoseTree siblings", 120 | %{simple_z: z} do 121 | assert Zipper.has_siblings?(z) == false 122 | end 123 | 124 | test "should return false when given bad values" do 125 | for {value, idx} <- @bad_zippers do 126 | assert Zipper.has_siblings?(value) == false, 127 | "Expected `false` for element at index #{idx}" 128 | end 129 | end 130 | end 131 | 132 | describe "new/2" do 133 | test "should raise an ArgumentError if `prev` has invalid elements", %{ 134 | simple_z: z 135 | } do 136 | previous_siblings = [z.focus, :bad_tree] 137 | 138 | assert_raise ArgumentError, fn -> Zipper.new(z.focus, prev: previous_siblings) end 139 | end 140 | 141 | test "should raise an ArgumentError if `next` has invalid elements", %{ 142 | simple_z: z 143 | } do 144 | next_siblings = [z.focus, :bad_tree] 145 | 146 | assert_raise ArgumentError, fn -> Zipper.new(z.focus, next: next_siblings) end 147 | end 148 | 149 | test "should raise an ArgumentError if `path` has invalid elements", %{ 150 | simple_z: z 151 | } do 152 | path = [Location.new(z.focus), :bad_location] 153 | 154 | assert_raise ArgumentError, fn -> Zipper.new(z.focus, path: path) end 155 | end 156 | end 157 | 158 | describe "root?/1" do 159 | test "should return false if given a Zipper with populated path", %{ 160 | simple_z: z 161 | } do 162 | path = [Location.new(z.focus)] 163 | 164 | new_z = %Zipper{z | path: path} 165 | 166 | assert Zipper.root?(new_z) == false 167 | end 168 | end 169 | 170 | describe "index_of_parent/1" do 171 | test "should return nil if given a Zipper with no parent", %{simple_z: z} do 172 | assert nil == Zipper.index_of_parent(z) 173 | end 174 | 175 | test "should return 0 if parent has no siblings", %{z_with_parent: z} do 176 | assert 0 == Zipper.index_of_parent(z) 177 | end 178 | end 179 | 180 | describe "index_of_grandparent/1" do 181 | test "should return nil if given a Zipper with no parent", %{simple_z: z} do 182 | assert nil == Zipper.index_of_grandparent(z) 183 | end 184 | 185 | test "should return nil if given a Zipper with no grandparent", %{z_with_parent: z} do 186 | assert nil == Zipper.index_of_grandparent(z) 187 | end 188 | 189 | test "should return 0 if grandparent has no siblings", %{z_with_grandparent: z} do 190 | assert 0 == Zipper.index_of_grandparent(z) 191 | end 192 | end 193 | 194 | describe "parent_location/1" do 195 | test "should return nil if given a root Zipper", %{simple_z: z} do 196 | assert nil == Zipper.parent_location(z) 197 | end 198 | end 199 | 200 | describe "parent_term/1" do 201 | test "should return nil if given a root Zipper", %{simple_z: z} do 202 | assert nil == Zipper.parent_term(z) 203 | end 204 | end 205 | 206 | describe "map_focus/2" do 207 | test "should raise ArgumentError if the map_fn returns a result that is not a ExRoseTree", %{ 208 | simple_z: z 209 | } do 210 | map_fn = fn tree_node -> tree_node.term * 2 end 211 | 212 | assert_raise(ArgumentError, fn -> Zipper.map_focus(z, map_fn) end) 213 | end 214 | end 215 | 216 | describe "remove_focus/1" do 217 | test "should return an empty zipper and nil when given an empty zipper", %{empty_z: z} do 218 | assert {^z, nil} = Zipper.remove_focus(z) 219 | end 220 | 221 | test "should return an empty zipper and nil when given a root leaf zipper", %{ 222 | empty_z: empty_z, 223 | leaf_z: z 224 | } do 225 | assert {^empty_z, nil} = Zipper.remove_focus(z) 226 | end 227 | 228 | test "should return a zipper focused on the parent with no children and the removed focus when given a zipper with parent and no siblings", 229 | %{z_with_parent: z} do 230 | expected_removal = z.focus 231 | 232 | assert {%Zipper{focus: focus}, ^expected_removal} = Zipper.remove_focus(z) 233 | assert focus.term == 10 234 | assert focus.children == [] 235 | end 236 | 237 | test "should return a zipper focused on the previous sibling and the removed focus when given a zipper with prev siblings but no next siblings", 238 | %{z_with_siblings: z} do 239 | z_with_removed_next = %{z | next: []} 240 | 241 | expected_removal = z.focus 242 | [expected_focus | expected_prev] = z.prev 243 | 244 | assert {%Zipper{} = actual_z, ^expected_removal} = Zipper.remove_focus(z_with_removed_next) 245 | assert actual_z.focus.term == expected_focus.term 246 | assert actual_z.prev == expected_prev 247 | assert actual_z.next == [] 248 | end 249 | 250 | test "should return a zipper focused on the next sibling and the removed focus when given a zipper with nex siblings", 251 | %{z_with_siblings: z} do 252 | expected_removal = z.focus 253 | [expected_focus | expected_next] = z.next 254 | 255 | assert {%Zipper{} = actual_z, ^expected_removal} = Zipper.remove_focus(z) 256 | assert actual_z.focus.term == expected_focus.term 257 | assert actual_z.next == expected_next 258 | assert actual_z.prev == z.prev 259 | end 260 | end 261 | 262 | describe "map_previous_siblings/2" do 263 | test "should raise ArgumentError if the map_fn returns a result that is not a ExRoseTree", %{ 264 | z_with_siblings: z 265 | } do 266 | map_fn = fn tree_node -> tree_node.term * 2 end 267 | 268 | assert_raise(ArgumentError, fn -> Zipper.map_previous_siblings(z, map_fn) end) 269 | end 270 | end 271 | 272 | describe "map_next_siblings/2" do 273 | test "should raise ArgumentError if the map_fn returns a result that is not a ExRoseTree", %{ 274 | z_with_siblings: z 275 | } do 276 | map_fn = fn tree_node -> tree_node.term * 2 end 277 | 278 | assert_raise(ArgumentError, fn -> Zipper.map_next_siblings(z, map_fn) end) 279 | end 280 | end 281 | 282 | describe "map_path/2" do 283 | test "should raise ArgumentError if the map_fn returns a result that is not a Location", %{ 284 | z_with_grandparent: z 285 | } do 286 | map_fn = fn location -> location.term * 2 end 287 | 288 | assert_raise(ArgumentError, fn -> Zipper.map_path(z, map_fn) end) 289 | end 290 | end 291 | end 292 | -------------------------------------------------------------------------------- /test/support/ex_rose_tree_case.ex: -------------------------------------------------------------------------------- 1 | defmodule ExRoseTree.ExRoseTreeCase do 2 | @moduledoc """ 3 | This module defines the setup for tests requiring 4 | access to pre-polutated `ExRoseTree` structs. 5 | """ 6 | 7 | use ExUnit.CaseTemplate 8 | 9 | alias ExRoseTree 10 | alias ExRoseTree.Support.Generators 11 | 12 | using do 13 | quote do 14 | import ExUnit.CaptureLog 15 | 16 | require Logger 17 | require ExRoseTree 18 | 19 | alias ExRoseTree 20 | alias ExRoseTree.Support.Generators 21 | end 22 | end 23 | 24 | setup do 25 | empty_tree = %ExRoseTree{term: nil, children: []} 26 | 27 | leaf_tree = %ExRoseTree{term: 1, children: []} 28 | 29 | simple_tree = %ExRoseTree{ 30 | term: 1, 31 | children: [ 32 | %ExRoseTree{term: 2, children: []}, 33 | %ExRoseTree{term: 3, children: []}, 34 | %ExRoseTree{term: 4, children: []} 35 | ] 36 | } 37 | 38 | tree_x5 = Generators.random_tree(total_nodes: 5) 39 | 40 | tree_x25 = Generators.random_tree(total_nodes: 25) 41 | 42 | tree_x100 = Generators.random_tree(total_nodes: 100) 43 | 44 | tree_x = Generators.random_tree() 45 | 46 | all_trees_with_idx = 47 | [ 48 | empty_tree, 49 | leaf_tree, 50 | simple_tree, 51 | tree_x5, 52 | tree_x25, 53 | tree_x100, 54 | tree_x 55 | ] 56 | |> Enum.with_index() 57 | 58 | %{ 59 | all_trees_with_idx: all_trees_with_idx, 60 | empty_tree: empty_tree, 61 | leaf_tree: leaf_tree, 62 | simple_tree: simple_tree, 63 | tree_x: tree_x, 64 | tree_x5: tree_x5, 65 | tree_x25: tree_x25, 66 | tree_x100: tree_x100 67 | } 68 | end 69 | end 70 | -------------------------------------------------------------------------------- /test/support/zipper_case.ex: -------------------------------------------------------------------------------- 1 | defmodule ZipperCase do 2 | @moduledoc """ 3 | This module defines the setup for tests requiring 4 | access to pre-populated `ExRoseTree.Zipper` 5 | structs. 6 | """ 7 | 8 | use ExUnit.CaseTemplate 9 | 10 | alias ExRoseTree.Zipper 11 | alias ExRoseTree.Support.Zippers 12 | 13 | using do 14 | quote do 15 | import ExUnit.CaptureLog 16 | 17 | require ExRoseTree 18 | require ExRoseTree.Zipper 19 | 20 | alias ExRoseTree 21 | alias ExRoseTree.{Util, Zipper} 22 | alias ExRoseTree.Zipper.Location 23 | alias ExRoseTree.Support.{Generators, Zippers} 24 | end 25 | end 26 | 27 | setup_all do 28 | %{ 29 | empty_z: Zippers.empty_z(), 30 | leaf_z: Zippers.leaf_z(), 31 | simple_z: Zippers.simple_z(), 32 | z_with_parent: Zippers.z_with_parent(), 33 | z_with_grandparent: Zippers.z_with_grandparent(), 34 | z_with_great_grandparent: Zippers.z_with_great_grandparent(), 35 | z_with_grandchildren: Zippers.z_with_grandchildren(), 36 | z_with_grandchildren_2: Zippers.z_with_grandchildren_2(), 37 | z_with_great_grandchildren: Zippers.z_with_great_grandchildren(), 38 | z_with_great_grandchildren_2: Zippers.z_with_great_grandchildren_2(), 39 | z_with_siblings: Zippers.z_with_siblings(), 40 | z_with_piblings: Zippers.z_with_piblings(), 41 | z_with_grandpiblings: Zippers.z_with_grandpiblings(), 42 | z_with_ancestral_piblings: Zippers.z_with_ancestral_piblings(), 43 | z_with_no_ancestral_piblings: Zippers.z_with_no_ancestral_piblings(), 44 | z_with_niblings: Zippers.z_with_niblings(), 45 | z_with_grand_niblings: Zippers.z_with_grand_niblings(), 46 | z_with_descendant_niblings: Zippers.z_with_descendant_niblings(), 47 | z_with_extended_niblings: Zippers.z_with_extended_niblings(), 48 | z_with_1st_cousins: Zippers.z_with_1st_cousins(), 49 | z_with_2nd_cousins: Zippers.z_with_2nd_cousins(), 50 | z_with_extended_cousins: Zippers.z_with_extended_cousins(), 51 | z_with_extended_cousins_2: Zippers.z_with_extended_cousins_2(), 52 | z_depth_first: Zippers.z_depth_first(), 53 | z_depth_first_siblings: Zippers.z_depth_first_siblings(), 54 | z_depth_first_siblings_at_end: Zipper.descend_to_last(Zippers.z_depth_first_siblings()), 55 | z_breadth_first: Zippers.z_breadth_first(), 56 | z_breadth_first_siblings: Zippers.z_breadth_first_siblings(), 57 | z_breadth_first_siblings_at_end: Zipper.forward_to_last(Zippers.z_breadth_first_siblings()) 58 | } 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | --------------------------------------------------------------------------------