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 a503bf7 commit 5e2be93
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 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
15 changes: 9 additions & 6 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
%{
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"cerlc": {:hex, :cerlc, "0.2.1", "cfe0880aa049ebcca079ca49578055aa48e7f2e9ed8ae08bd1f919d59015d03f", [:rebar3], [], "hexpm", "37f0d74a4277dcbaf64c7c47e9953dcc8060d26d86bb466ab2a86928e0181a7d"},
"circuits_i2c": {:hex, :circuits_i2c, "2.0.2", "ed1bac41cca0b777e11e2105c08dc7cad67a221e0d7eec1680a644eba677f9f5", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "b8cf6ef783f9184bf4e45a1a4fa0a72523aa4e3c2fa980f94a45336c92d95178"},
"credo": {:hex, :credo, "1.7.3", "05bb11eaf2f2b8db370ecaa6a6bda2ec49b2acd5e0418bc106b73b07128c0436", [: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", "35ea675a094c934c22fb1dca3696f3c31f2728ae6ef5a53b5d648c11180a4535"},
"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"},
"earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"},
"elixir_make": {:hex, :elixir_make, "0.7.7", "7128c60c2476019ed978210c245badf08b03dbec4f24d05790ef791da11aa17c", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "5bc19fff950fad52bbe5f211b12db9ec82c6b34a9647da0c2224b8b8464c7e6c"},
"elixir_make": {:hex, :elixir_make, "0.8.3", "d38d7ee1578d722d89b4d452a3e36bcfdc644c618f0d063b874661876e708683", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.0", [hex: :certifi, repo: "hexpm", optional: true]}], "hexpm", "5c99a18571a756d4af7a4d89ca75c28ac899e6103af6f223982f09ce44942cc9"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.31.1", "8a2355ac42b1cc7b2379da9e40243f2670143721dd50748bf6c3b1184dae2089", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.1", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "3178c3a407c557d8343479e1ff117a96fd31bafe52a039079593fb0524ef61b0"},
"ex_doc": {:hex, :ex_doc, "0.32.1", "21e40f939515373bcdc9cffe65f3b3543f05015ac6c3d01d991874129d173420", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.1", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "5142c9db521f106d61ff33250f779807ed2a88620e472ac95dc7d59c380113da"},
"file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.3", "d684f4bac8690e70b06eb52dad65d26de2eefa44cd19d64a8095e1417df7c8fd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "b78dc853d2e670ff6390b605d807263bf606da3c82be37f9d7f68635bd886fc9"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.5", "e0ff5a7c708dda34311f7522a8758e23bfcd7d8d8068dc312b5eb41c6fd76eba", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "94d2e986428585a21516d7d7149781480013c56e30c6a233534bedf38867a59a"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
"typed_struct": {:hex, :typed_struct, "0.3.0", "939789e3c1dca39d7170c87f729127469d1315dcf99fee8e152bb774b17e7ff7", [:mix], [], "hexpm", "c50bd5c3a61fe4e198a8504f939be3d3c85903b382bde4865579bc23111d1b6d"},
}
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 5e2be93

Please sign in to comment.