From ddd3b8ed39256bb1087d13e9a438255511cf683a Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 4 Feb 2022 08:17:11 -0400 Subject: [PATCH] bob --- bob/.exercism/config.json | 46 +++++++++++ bob/.exercism/metadata.json | 1 + bob/.formatter.exs | 4 + bob/.gitignore | 24 ++++++ bob/HELP.md | 75 ++++++++++++++++++ bob/README.md | 59 ++++++++++++++ bob/lib/bob.ex | 17 ++++ bob/mix.exs | 28 +++++++ bob/test/bob_test.exs | 115 ++++++++++++++++++++++++++++ bob/test/test_helper.exs | 2 + name-badge/.exercism/config.json | 25 ++++++ name-badge/.exercism/metadata.json | 1 + name-badge/.formatter.exs | 4 + name-badge/.gitignore | 24 ++++++ name-badge/HELP.md | 75 ++++++++++++++++++ name-badge/HINTS.md | 25 ++++++ name-badge/README.md | 104 +++++++++++++++++++++++++ name-badge/lib/name_badge.ex | 13 ++++ name-badge/mix.exs | 28 +++++++ name-badge/test/name_badge_test.exs | 33 ++++++++ name-badge/test/test_helper.exs | 2 + 21 files changed, 705 insertions(+) create mode 100644 bob/.exercism/config.json create mode 100644 bob/.exercism/metadata.json create mode 100644 bob/.formatter.exs create mode 100644 bob/.gitignore create mode 100644 bob/HELP.md create mode 100644 bob/README.md create mode 100644 bob/lib/bob.ex create mode 100644 bob/mix.exs create mode 100644 bob/test/bob_test.exs create mode 100644 bob/test/test_helper.exs create mode 100644 name-badge/.exercism/config.json create mode 100644 name-badge/.exercism/metadata.json create mode 100644 name-badge/.formatter.exs create mode 100644 name-badge/.gitignore create mode 100644 name-badge/HELP.md create mode 100644 name-badge/HINTS.md create mode 100644 name-badge/README.md create mode 100644 name-badge/lib/name_badge.ex create mode 100644 name-badge/mix.exs create mode 100644 name-badge/test/name_badge_test.exs create mode 100644 name-badge/test/test_helper.exs diff --git a/bob/.exercism/config.json b/bob/.exercism/config.json new file mode 100644 index 0000000..83269cb --- /dev/null +++ b/bob/.exercism/config.json @@ -0,0 +1,46 @@ +{ + "blurb": "Bob is a lackadaisical teenager. In conversation, his responses are very limited.", + "authors": [ + "rubysolo" + ], + "contributors": [ + "andrewsardone", + "angelikatyborska", + "austinlyons", + "cbliard", + "Cohen-Carlisle", + "dalexj", + "devonestes", + "digitalronin", + "doncruse", + "etrepum", + "ghajba", + "kytrinyx", + "lpil", + "neenjaw", + "parkerl", + "pminten", + "rsslldnphy", + "seeflanigan", + "sotojuan", + "Teapane", + "tjcelaya", + "Tonkpils", + "victorpre", + "vladimir-tikhonov", + "waiting-for-dev" + ], + "files": { + "solution": [ + "lib/bob.ex" + ], + "test": [ + "test/bob_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "source": "Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial.", + "source_url": "http://pine.fm/LearnToProgram/?Chapter=06" +} diff --git a/bob/.exercism/metadata.json b/bob/.exercism/metadata.json new file mode 100644 index 0000000..733b4da --- /dev/null +++ b/bob/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"bob","id":"d402d783bcca42a59df2a01c8d291ebe","url":"https://exercism.org/tracks/elixir/exercises/bob","handle":"rjNemo","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/bob/.formatter.exs b/bob/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/bob/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/bob/.gitignore b/bob/.gitignore new file mode 100644 index 0000000..9dfd16d --- /dev/null +++ b/bob/.gitignore @@ -0,0 +1,24 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +bob-*.tar + diff --git a/bob/HELP.md b/bob/HELP.md new file mode 100644 index 0000000..dfb164b --- /dev/null +++ b/bob/HELP.md @@ -0,0 +1,75 @@ +# Help + +## Running the tests + +From the terminal, change to the base directory of the exercise then execute the tests with: + +```bash +$ mix test +``` + +This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs` + +Documentation: + +* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html) +* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html) + +## Pending tests + +In test suites of practice exercises, all but the first test have been tagged to be skipped. + +Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol. + +For example: + +```elixir +# @tag :pending +test "shouting" do + assert Bob.hey("WATCH OUT!") == "Whoa, chill out!" +end +``` + +If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command: + +```bash +$ mix test --include pending +``` + +Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`. + +```elixir +# ExUnit.configure(exclude: :pending, trace: true) +``` + +## Useful `mix test` options + +* `test/.exs:LINENUM` - runs only a single test, the test from `.exs` whose definition is on line `LINENUM` +* `--failed` - runs only tests that failed the last time they ran +* `--max-failures` - the suite stops evaluating tests when this number of test failures +is reached +* `--seed 0` - disables randomization so the tests in a single file will always be ran +in the same order they were defined in + +## Submitting your solution + +You can submit your solution using the `exercism submit lib/bob.ex` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found. +If you can't find what you're looking for in the documentation, feel free to ask help in the Exercism's BEAM [gitter channel](https://gitter.im/exercism/xerlang). \ No newline at end of file diff --git a/bob/README.md b/bob/README.md new file mode 100644 index 0000000..8798038 --- /dev/null +++ b/bob/README.md @@ -0,0 +1,59 @@ +# Bob + +Welcome to Bob on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Bob is a lackadaisical teenager. In conversation, his responses are very limited. + +Bob answers 'Sure.' if you ask him a question, such as "How are you?". + +He answers 'Whoa, chill out!' if you YELL AT HIM (in all capitals). + +He answers 'Calm down, I know what I'm doing!' if you yell a question at him. + +He says 'Fine. Be that way!' if you address him without actually saying +anything. + +He answers 'Whatever.' to anything else. + +Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English. + +## Source + +### Created by + +- @rubysolo + +### Contributed to by + +- @andrewsardone +- @angelikatyborska +- @austinlyons +- @cbliard +- @Cohen-Carlisle +- @dalexj +- @devonestes +- @digitalronin +- @doncruse +- @etrepum +- @ghajba +- @kytrinyx +- @lpil +- @neenjaw +- @parkerl +- @pminten +- @rsslldnphy +- @seeflanigan +- @sotojuan +- @Teapane +- @tjcelaya +- @Tonkpils +- @victorpre +- @vladimir-tikhonov +- @waiting-for-dev + +### Based on + +Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. - http://pine.fm/LearnToProgram/?Chapter=06 \ No newline at end of file diff --git a/bob/lib/bob.ex b/bob/lib/bob.ex new file mode 100644 index 0000000..eada45f --- /dev/null +++ b/bob/lib/bob.ex @@ -0,0 +1,17 @@ +defmodule Bob do + @spec hey(String.t()) :: String.t() + def hey(input) do + letter? = &(String.upcase(&1) != String.downcase(&1)) + shouting? = &(&1 == String.upcase(&1) and letter?.(&1)) + question? = &(String.trim(&1) |> String.ends_with?("?")) + blank? = &(String.trim(&1) == "") + + cond do + blank?.(input) -> "Fine. Be that way!" + shouting?.(input) and question?.(input) -> "Calm down, I know what I'm doing!" + shouting?.(input) -> "Whoa, chill out!" + question?.(input) -> "Sure." + true -> "Whatever." + end + end +end diff --git a/bob/mix.exs b/bob/mix.exs new file mode 100644 index 0000000..68259d0 --- /dev/null +++ b/bob/mix.exs @@ -0,0 +1,28 @@ +defmodule Bob.MixProject do + use Mix.Project + + def project do + [ + app: :bob, + version: "0.1.0", + # elixir: "~> 1.8", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/bob/test/bob_test.exs b/bob/test/bob_test.exs new file mode 100644 index 0000000..f3c1062 --- /dev/null +++ b/bob/test/bob_test.exs @@ -0,0 +1,115 @@ +defmodule BobTest do + use ExUnit.Case + + test "stating something" do + assert Bob.hey("Tom-ay-to, tom-aaaah-to.") == "Whatever." + end + + test "shouting" do + assert Bob.hey("WATCH OUT!") == "Whoa, chill out!" + end + + test "shouting gibberish" do + assert Bob.hey("FCECDFCAAB") == "Whoa, chill out!" + end + + test "asking a question" do + assert Bob.hey("Does this cryogenic chamber make me look fat?") == "Sure." + end + + test "asking a numeric question" do + assert Bob.hey("You are, what, like 15?") == "Sure." + end + + test "asking gibberish" do + assert Bob.hey("fffbbcbeab?") == "Sure." + end + + test "talking forcefully" do + assert Bob.hey("Hi there!") == "Whatever." + end + + test "using acronyms in regular speech" do + assert Bob.hey("It's OK if you don't want to go to the DMV.") == "Whatever." + end + + test "talking in capitals" do + assert Bob.hey("This Isn't Shouting!") == "Whatever." + end + + test "forceful question" do + assert Bob.hey("WHAT'S GOING ON?") == "Calm down, I know what I'm doing!" + end + + test "asking in capitals" do + assert Bob.hey("THIS ISN'T SHOUTING?") == "Calm down, I know what I'm doing!" + end + + test "shouting numbers" do + assert Bob.hey("1, 2, 3 GO!") == "Whoa, chill out!" + end + + test "shouting with special characters" do + assert Bob.hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!") == "Whoa, chill out!" + end + + test "shouting with no exclamation mark" do + assert Bob.hey("I HATE THE DENTIST") == "Whoa, chill out!" + end + + test "statement containing question mark" do + assert Bob.hey("Ending with ? means a question.") == "Whatever." + end + + test "silence" do + assert Bob.hey("") == "Fine. Be that way!" + end + + test "prolonged silence" do + assert Bob.hey(" ") == "Fine. Be that way!" + end + + test "alternate silence" do + assert Bob.hey("\t\t\t\t\t\t\t\t\t\t") == "Fine. Be that way!" + end + + test "only numbers" do + assert Bob.hey("1, 2, 3") == "Whatever." + end + + test "multiple line question" do + assert Bob.hey("\nDoes this cryogenic chamber make me look fat?\nNo.") == "Whatever." + end + + test "question with numbers" do + assert Bob.hey("4?") == "Sure." + end + + test "non-letters with question" do + assert Bob.hey(":) ?") == "Sure." + end + + test "prattling on" do + assert Bob.hey("Wait! Hang on. Are you going to be OK?") == "Sure." + end + + test "starting with whitespace" do + assert Bob.hey(" hmmmmmmm...") == "Whatever." + end + + test "ending with whitespace" do + assert Bob.hey("Okay if like my spacebar quite a bit? ") == "Sure." + end + + test "other whitespace" do + assert Bob.hey("\n\r \t") == "Fine. Be that way!" + end + + test "non-question ending with whitespace" do + assert Bob.hey("This is a statement ending with whitespace ") == "Whatever." + end + + test "shouting in Russian" do + assert Bob.hey("УХОДИ") == "Whoa, chill out!" + end +end diff --git a/bob/test/test_helper.exs b/bob/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/bob/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true) diff --git a/name-badge/.exercism/config.json b/name-badge/.exercism/config.json new file mode 100644 index 0000000..79e8362 --- /dev/null +++ b/name-badge/.exercism/config.json @@ -0,0 +1,25 @@ +{ + "blurb": "Learn about nil and the if conditional expression by printing name badges for factory employees.", + "icon": "tim-from-marketing", + "authors": [ + "angelikatyborska" + ], + "contributors": [ + "neenjaw" + ], + "files": { + "solution": [ + "lib/name_badge.ex" + ], + "test": [ + "test/name_badge_test.exs" + ], + "exemplar": [ + ".meta/exemplar.ex" + ] + }, + "forked_from": [ + "csharp/tim-from-marketing" + ], + "language_versions": ">=1.10" +} diff --git a/name-badge/.exercism/metadata.json b/name-badge/.exercism/metadata.json new file mode 100644 index 0000000..68a546f --- /dev/null +++ b/name-badge/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"name-badge","id":"f85b910f260242008c01c4b1e66fe785","url":"https://exercism.org/tracks/elixir/exercises/name-badge","handle":"rjNemo","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/name-badge/.formatter.exs b/name-badge/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/name-badge/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/name-badge/.gitignore b/name-badge/.gitignore new file mode 100644 index 0000000..cb54963 --- /dev/null +++ b/name-badge/.gitignore @@ -0,0 +1,24 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +nil-*.tar + diff --git a/name-badge/HELP.md b/name-badge/HELP.md new file mode 100644 index 0000000..2cf29a7 --- /dev/null +++ b/name-badge/HELP.md @@ -0,0 +1,75 @@ +# Help + +## Running the tests + +From the terminal, change to the base directory of the exercise then execute the tests with: + +```bash +$ mix test +``` + +This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs` + +Documentation: + +* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html) +* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html) + +## Pending tests + +In test suites of practice exercises, all but the first test have been tagged to be skipped. + +Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol. + +For example: + +```elixir +# @tag :pending +test "shouting" do + assert Bob.hey("WATCH OUT!") == "Whoa, chill out!" +end +``` + +If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command: + +```bash +$ mix test --include pending +``` + +Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`. + +```elixir +# ExUnit.configure(exclude: :pending, trace: true) +``` + +## Useful `mix test` options + +* `test/.exs:LINENUM` - runs only a single test, the test from `.exs` whose definition is on line `LINENUM` +* `--failed` - runs only tests that failed the last time they ran +* `--max-failures` - the suite stops evaluating tests when this number of test failures +is reached +* `--seed 0` - disables randomization so the tests in a single file will always be ran +in the same order they were defined in + +## Submitting your solution + +You can submit your solution using the `exercism submit lib/name_badge.ex` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found. +If you can't find what you're looking for in the documentation, feel free to ask help in the Exercism's BEAM [gitter channel](https://gitter.im/exercism/xerlang). \ No newline at end of file diff --git a/name-badge/HINTS.md b/name-badge/HINTS.md new file mode 100644 index 0000000..19f264b --- /dev/null +++ b/name-badge/HINTS.md @@ -0,0 +1,25 @@ +# Hints + +## General + +- Read about `if` in the official [Getting Started guide][getting-started-if-unless] or on [elixirschool.com][elixirschool-if-unless]. + +## 1. Print a badge for an employee + +- This is a base case where we can assume that none of the input data is `nil`. +- If you need to refresh your memory about working with strings, read about them in the official [Getting Started guide][getting-started-basic-strings]. + +## 2. Print a badge for a new employee + +- Strings are always _truthy_ and `nil` is _falsy_. +- There is a [macro][kernel-if] that returns one of the two options, depending of whether it was given a _truthy_ or a _falsy_ value. + +## 3. Print a badge for the owner + +- Strings are always _truthy_ and `nil` is _falsy_. +- There is a [macro][kernel-if] that returns one of the two options, depending of whether it was given a _truthy_ or a _falsy_ value. + +[kernel-if]: https://hexdocs.pm/elixir/Kernel.html#if/2 +[getting-started-basic-strings]: https://elixir-lang.org/getting-started/basic-types.html#strings +[getting-started-if-unless]: https://elixir-lang.org/getting-started/case-cond-and-if.html#if-and-unless +[elixir-school-if-unless]: https://elixirschool.com/en/lessons/basics/control-structures/#if-and-unless \ No newline at end of file diff --git a/name-badge/README.md b/name-badge/README.md new file mode 100644 index 0000000..795d3b2 --- /dev/null +++ b/name-badge/README.md @@ -0,0 +1,104 @@ +# Name Badge + +Welcome to Name Badge on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. +If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) + +## Introduction + +## Nil + +[Nil][nil-dictionary] is an English word meaning "nothing" or "zero". In Elixir, `nil` is a special value that means an _absence_ of a value. + +```elixir +# I do not have a favorite color +favorite_color = nil +``` + +In other programming languages, `null` or `none` values might play a similar role. + +## If + +Besides `cond`, Elixir also provides the macro [`if/2`][getting-started-if-unless] which is useful when you need to check for only one condition. + +[`if/2`][kernel-if] accepts a condition and two options. It returns the first option if the condition is _truthy_, and the second option if the condition is _falsy_. + +```elixir +age = 15 + +if age >= 16 do + "You are allowed to drink beer in Germany." +else + "No beer for you!" +end + +# => "No beer for you!" +``` + +It is also possible to write an `if` expression on a single line. Note the comma after the condition. + +```elixir +if age > 16, do: "beer", else: "no beer" +``` + +This syntax is helpful for very short expressions, but should be avoided if the expression won't fit on a single line. + +### _Truthy_ and _falsy_ + +In Elixir, all data types evaluate to a _truthy_ or _falsy_ value when they are encountered in a boolean context (like an `if` expression). All data is considered _truthy_ **except** for `false` and `nil`. In particular, empty strings, the integer `0`, and empty lists are all considered _truthy_ in Elixir. + +[nil-dictionary]: https://www.merriam-webster.com/dictionary/nil +[kernel-if]: https://hexdocs.pm/elixir/Kernel.html#if/2 +[getting-started-if-unless]: https://elixir-lang.org/getting-started/case-cond-and-if.html#if-and-unless + +## Instructions + +In this exercise you'll be writing code to print name badges for factory employees. Employees have an ID, name, and department name. Employee badge labels are formatted as follows: `"[id] - name - DEPARTMENT"`. + +## 1. Print a badge for an employee + +Implement the `NameBadge.print/3` function. It should take an id, name, and a department. It should return the badge label, with the department name in uppercase. + +```elixir +NameBadge.print(67, "Katherine Williams", "Strategic Communication") +# => "[67] - Katherine Williams - STRATEGIC COMMUNICATION" +``` + +## 2. Print a badge for a new employee + +Due to a quirk in the computer system, new employees occasionally don't yet have an ID when they start working at the factory. As badges are required, they will receive a temporary badge without the ID prefix. + +Extend the `NameBadge.print/3` function. When the id is missing, it should print a badge without it. + +```elixir +NameBadge.print(nil, "Robert Johnson", "Procurement") +# => "Robert Johnson - PROCUREMENT" +``` + +## 3. Print a badge for the owner + +Even the factory's owner has to wear a badge at all times. However, an owner does not have a department. In this case, the label should print `"OWNER"` instead of the department name. + +Extend the `NameBadge.print/3` function. When the department is missing, assume the badge belongs to the company owner. + +```elixir +NameBadge.print(204, "Rachel Miller", nil) +# => "[204] - Rachel Miller - OWNER" +``` + +Note that it is possible for the owner to also be a new employee. + +```elixir +NameBadge.print(nil, "Rachel Miller", nil) +# => "Rachel Miller - OWNER" +``` + +## Source + +### Created by + +- @angelikatyborska + +### Contributed to by + +- @neenjaw \ No newline at end of file diff --git a/name-badge/lib/name_badge.ex b/name-badge/lib/name_badge.ex new file mode 100644 index 0000000..8e18a02 --- /dev/null +++ b/name-badge/lib/name_badge.ex @@ -0,0 +1,13 @@ +defmodule NameBadge do + def print(id, name, nil) do + if id do + "[#{id}] - #{name} - OWNER" + else + "#{name} - OWNER" + end + end + + def print(nil, name, department), do: "#{name} - #{String.upcase(department)}" + + def print(id, name, department), do: "[#{id}] - #{name} - #{String.upcase(department)}" +end diff --git a/name-badge/mix.exs b/name-badge/mix.exs new file mode 100644 index 0000000..2709c17 --- /dev/null +++ b/name-badge/mix.exs @@ -0,0 +1,28 @@ +defmodule NameBadge.MixProject do + use Mix.Project + + def project do + [ + app: :name_badge, + version: "0.1.0", + # elixir: "~> 1.10", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/name-badge/test/name_badge_test.exs b/name-badge/test/name_badge_test.exs new file mode 100644 index 0000000..3201483 --- /dev/null +++ b/name-badge/test/name_badge_test.exs @@ -0,0 +1,33 @@ +defmodule NameBadgeTest do + use ExUnit.Case + doctest NameBadge + + describe "print/3" do + @tag task_id: 1 + test "prints the employee badge with full data" do + assert NameBadge.print(455, "Mary M. Brown", "MARKETING") == + "[455] - Mary M. Brown - MARKETING" + end + + @tag task_id: 1 + test "uppercases the department" do + assert NameBadge.print(89, "Jack McGregor", "Procurement") == + "[89] - Jack McGregor - PROCUREMENT" + end + + @tag task_id: 2 + test "prints the employee badge without id" do + assert NameBadge.print(nil, "Barbara White", "Security") == "Barbara White - SECURITY" + end + + @tag task_id: 3 + test "prints the owner badge" do + assert NameBadge.print(1, "Anna Johnson", nil) == "[1] - Anna Johnson - OWNER" + end + + @tag task_id: 3 + test "prints the owner badge without id" do + assert NameBadge.print(nil, "Stephen Dann", nil) == "Stephen Dann - OWNER" + end + end +end diff --git a/name-badge/test/test_helper.exs b/name-badge/test/test_helper.exs new file mode 100644 index 0000000..e8677a3 --- /dev/null +++ b/name-badge/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true, seed: 0)