daily pastebin goal
56%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -module(x_version_4).
  2.  
  3. -import(util_record, [insert_version/2,
  4.                       update_version/2,
  5.                       insert_field/3,
  6.                       update_field/3,
  7.                       remove_field/2,
  8.                       update_record/3]).
  9.  
  10. -include_lib("eunit/include/eunit.hrl").
  11.  
  12. -compile(export_all).
  13.  
  14.  
  15. % -record(x, {field_a                :: string()}).
  16.  
  17. % -record(x, {version = {version, 1} :: {version, integer()},
  18. %             field_a                :: string()}).
  19.  
  20. % -record(x, {version = {version, 2} :: {version, integer()},
  21. %             field_a                :: string(),
  22. %             field_b                :: string()}).
  23.  
  24. % -record(x, {version = {version, 3} :: {version, integer()},
  25. %             field_b                :: string()}).
  26.  
  27. -record(x, {version = {version, 4} :: {version, integer()},
  28.              field_b                :: atom()}).
  29.  
  30.  
  31. -spec update_x(not_versioned | integer(), tuple()) -> #x{}.
  32. update_x(not_versioned, X) ->
  33.     update_x(1, insert_version(1, X));
  34. update_x(1, X) ->
  35.     update_x(2, update_version(2, insert_field(3, "default_field_b", X)));
  36. update_x(2, X) ->
  37.     update_x(3, update_version(3, remove_field(2, X)));
  38. update_x(3, X) ->
  39.     update_x(4, update_version(4, update_field(2, fun list_to_atom/1, X)));
  40. update_x(4, X) ->
  41.     X.
  42.  
  43. -spec update_x(tuple()) -> #x{}.
  44. update_x(X) ->
  45.     update_record(x, fun update_x/2, X).
  46.  
  47.  
  48. update_x_not_versioned_test() ->
  49.     ?assertEqual(#x{version = {version, 4},
  50.                     field_b = default_field_b},
  51.                  update_x({x, "field_a"})).
  52.  
  53. update_x_version_1_test() ->
  54.     ?assertEqual(#x{version = {version, 4},
  55.                     field_b = default_field_b},
  56.                  update_x({x, {version, 1}, "field_a"})).
  57.  
  58. update_x_version_2_test() ->
  59.     ?assertEqual(#x{version = {version, 4},
  60.                     field_b = field_b},
  61.                  update_x({x, {version, 2}, "field_a", "field_b"})).
  62.  
  63. update_x_version_3_test() ->
  64.     ?assertEqual(#x{version = {version, 4},
  65.                     field_b = field_b},
  66.                  update_x({x, {version, 3}, "field_b"})).
  67.  
  68. update_x_version_4_test() ->
  69.     ?assertEqual(#x{version = {version, 4},
  70.                     field_b = field_b},
  71.                  update_x({x, {version, 4}, 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