Skip to content

Commit

Permalink
Use the circuits_sim to add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fhunleth committed May 1, 2024
1 parent 5bf4a37 commit 3182987
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Config

# Simulate an SHT4X on i2c-1
config :circuits_sim,
config: [
{CircuitsSim.Device.SHT4X, bus_name: "i2c-1", address: 0x44, serial_number: 0x87654321}
]

# Enable simulated I2C as the default
config :circuits_i2c, default_backend: CircuitsSim.I2C.Backend
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ defmodule SHT4X.MixProject do
{:credo, "~> 1.6", only: :lint, runtime: false},
{:credo_binary_patterns, "~> 0.2.2", only: :lint, runtime: false},
{:dialyxir, "~> 1.1", only: :lint, runtime: false},
{:circuits_sim, "~> 0.1.0", only: [:dev, :test]},
{:ex_doc, "~> 0.28", only: :docs, runtime: false},
{:cerlc, "~> 0.2.0"},
{:typed_struct, "~> 0.3.0"}
Expand Down
3 changes: 3 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
%{
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"cerlc": {:hex, :cerlc, "0.2.1", "cfe0880aa049ebcca079ca49578055aa48e7f2e9ed8ae08bd1f919d59015d03f", [:rebar3], [], "hexpm", "37f0d74a4277dcbaf64c7c47e9953dcc8060d26d86bb466ab2a86928e0181a7d"},
"circuits_gpio": {:hex, :circuits_gpio, "2.1.0", "8f31c2d255ac4a8e27481a1a93a4d850ba1f28f82209f4088bda0cb9ae802144", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "c8ad86bafeb81d027e9bf7d6f0c5a7029a86d96ebfb21921ef8cd5549f2a9238"},
"circuits_i2c": {:hex, :circuits_i2c, "2.0.4", "d08568730ca8a8237d0221b0fdf81f3e2e20385e449d4aa70fcb4c90b3b6543c", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "fbfe2630bc30944aab7920818f1a2bf8a18acc1148385c947990aa0724f52f36"},
"circuits_sim": {:hex, :circuits_sim, "0.1.0", "c8c23a7d738386d2acd2c2632436c99293076c0d294ee6607c72ea14e0f87c1d", [:mix], [{:cerlc, "~> 0.2.1", [hex: :cerlc, repo: "hexpm", optional: false]}, {:circuits_gpio, "~> 2.0", [hex: :circuits_gpio, repo: "hexpm", optional: false]}, {:circuits_i2c, "~> 2.0", [hex: :circuits_i2c, repo: "hexpm", optional: false]}, {:circuits_spi, "~> 2.0", [hex: :circuits_spi, repo: "hexpm", optional: false]}], "hexpm", "a34dd1a2fa66421f39cc233d0e16083c9d978bd5ba6735d109e2cf5b54b89cd5"},
"circuits_spi": {:hex, :circuits_spi, "2.0.3", "31ba832d786227e9c9241fa797f52eedafc38ba486a4813fff484b14a5d8edcb", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "77ccb7ebaa63d5969f6f6738396c736bc532ab92582dab8d75c2ae4e6f7f6ad3"},
"credo": {:hex, :credo, "1.7.5", "643213503b1c766ec0496d828c90c424471ea54da77c8a168c725686377b9545", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "f799e9b5cd1891577d8c773d245668aa74a2fcd15eb277f51a0131690ebfb3fd"},
"credo_binary_patterns": {:hex, :credo_binary_patterns, "0.2.3", "0dabadbe3cfd8db14b69ff346c112bfadde9bf65dc7aea19c39743c8d2ed07fa", [:mix], [{:credo, "~> 1.6", [hex: :credo, repo: "hexpm", optional: false]}], "hexpm", "3c333a564ed3e27f5c9f69985a921b88ef90f131bf722d085957cc4b25b7a085"},
"dialyxir": {:hex, :dialyxir, "1.4.3", "edd0124f358f0b9e95bfe53a9fcf806d615d8f838e2202a9f430d59566b6b53b", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "bf2cfb75cd5c5006bec30141b131663299c661a864ec7fbbc72dfa557487a986"},
Expand Down
37 changes: 37 additions & 0 deletions test/sht4x_test.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
defmodule SHT4XTest do
use ExUnit.Case
doctest SHT4X

alias CircuitsSim.Device.SHT4X, as: SHT4XSim

@i2c_bus "i2c-1"
@i2c_address 0x44

test "reading the temperature and humidity via the simulator" do
sht_pid = start_supervised!(SHT4X)

SHT4XSim.set_temperature_c(@i2c_bus, @i2c_address, 11.1)
SHT4XSim.set_humidity_rh(@i2c_bus, @i2c_address, 33.3)

measurement = SHT4X.get_sample(sht_pid)
assert_in_delta measurement.humidity_rh, 33.3, 0.1
assert_in_delta measurement.temperature_c, 11.1, 0.1
end

test "reading the simulated serial number" do
sht_pid = start_supervised!(SHT4X)

# See config.exs for where the serial number is set in the simulator
assert SHT4X.serial_number(sht_pid) == {:ok, 0x87654321}
end

defp add_degree(measurement) do
%{measurement | temperature_c: measurement.temperature_c + 1}
end

test "compensation callback gets called" do
sht_pid = start_supervised!({SHT4X, compensation_callback: &add_degree/1})

SHT4XSim.set_temperature_c(@i2c_bus, @i2c_address, 20)

measurement = SHT4X.get_sample(sht_pid)
assert_in_delta measurement.temperature_c, 21.0, 0.1
end
end

0 comments on commit 3182987

Please sign in to comment.