daily pastebin goal
56%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -module(x_version_3).
  2.  
  3. -import(util_record, [insert_version/2,
  4.                       update_version/2,
  5.                       insert_field/3,
  6.                       remove_field/2,
  7.                       update_record/3]).
  8.  
  9. -include_lib("eunit/include/eunit.hrl").
  10.  
  11. -compile(export_all).
  12.  
  13.  
  14. % -record(x, {field_a                :: string()}).
  15.  
  16. % -record(x, {version = {version, 1} :: {version, integer()},
  17. %             field_a                :: string()}).
  18.  
  19. % -record(x, {version = {version, 2} :: {version, integer()},
  20. %             field_a                :: string(),
  21. %             field_b                :: string()}).
  22.  
  23. -record(x, {version = {version, 3} :: {version, integer()},
  24.             field_b                :: string()}).
  25.  
  26.  
  27. -spec update_x(not_versioned | integer(), tuple()) -> #x{}.
  28. update_x(not_versioned, X) ->
  29.     update_x(1, insert_version(1, X));
  30. update_x(1, X) ->
  31.     update_x(2, update_version(2, insert_field(3, "default_field_b", X)));
  32. update_x(2, X) ->
  33.     update_x(3, update_version(3, remove_field(2, X)));
  34. update_x(3, X) ->
  35.     X.
  36.  
  37. -spec update_x(tuple()) -> #x{}.
  38. update_x(X) ->
  39.     update_record(x, fun update_x/2, X).
  40.  
  41.  
  42. update_x_not_versioned_test() ->
  43.     ?assertEqual(#x{version = {version, 3},
  44.                     field_b = "default_field_b"},
  45.                  update_x({x, "field_a"})).
  46.  
  47. update_x_version_1_test() ->
  48.     ?assertEqual(#x{version = {version, 3},
  49.                     field_b = "default_field_b"},
  50.                  update_x({x, {version, 1}, "field_a"})).
  51.  
  52. update_x_version_2_test() ->
  53.     ?assertEqual(#x{version = {version, 3},
  54.                     field_b = "field_b"},
  55.                  update_x({x, {version, 2}, "field_a", "field_b"})).
  56.  
  57. update_x_version_3_test() ->
  58.     ?assertEqual(#x{version = {version, 3},
  59.                     field_b = "field_b"},
  60.                  update_x({x, {version, 3}, "field_b"})).
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top