extern crate web_wars_engine; use web_wars_engine::actions; use web_wars_engine::models::action::{Action, ActionKind}; use web_wars_engine::models::game::{Game, MapSettings, Weather}; use web_wars_engine::models::user::UserData; // apply #[test] pub fn sets_weather_by_param() { let mut game = Game { active: true, user_data: vec![ UserData { ..Default::default() }, UserData { ..Default::default() } ], ..Default::default() }; let action = Action { kind: ActionKind::Weather, params: vec![serde_json::to_string(&Weather::Clear).unwrap(), "1".to_owned()], turn: 0, round: 0, ..Default::default() }; let result = actions::apply(action, game).unwrap(); game = result.game; assert_eq!(game.weather, Weather::Clear); assert_eq!(game.weather_turn, Some(2)); } #[test] pub fn determine_weather() { let game = Game { weather: Weather::Clear, map_settings: MapSettings { starting_weather: Weather::Random, weather_seed: Some("sunny skies all day long".to_owned()), ..Default::default() }, ..Default::default() }; assert_eq!(None, actions::weather::determine_weather(&game, 0, 0)); assert_eq!(Some(Weather::Rain), actions::weather::determine_weather(&game, 0, 1)); assert_eq!(None, actions::weather::determine_weather(&game, 0, 2)); assert_eq!(Some(Weather::Rain), actions::weather::determine_weather(&game, 0, 3)); assert_eq!(None, actions::weather::determine_weather(&game, 0, 4)); assert_eq!(None, actions::weather::determine_weather(&game, 0, 5)); assert_eq!(None, actions::weather::determine_weather(&game, 0, 6)); assert_eq!(None, actions::weather::determine_weather(&game, 0, 7)); assert_eq!(Some(Weather::Snow), actions::weather::determine_weather(&game, 0, 8)); assert_eq!(None, actions::weather::determine_weather(&game, 0, 9)); assert_eq!(None, actions::weather::determine_weather(&game, 0, 10)); assert_eq!(None, actions::weather::determine_weather(&game, 0, 11)); assert_eq!(None, actions::weather::determine_weather(&game, 0, 12)); } // test "create_action/1 kind: weather, valid" { // user = insert(:user) // commander = insert(:commander) // user_data = build(:user_data, %{ // turn: true, // user_id: user.id, // commander_id: commander.id // }) // game = insert(:game, %{ // weather: "Clear", // user_data: [user_data] // }) // action = build(:action, %{ // kind: "weather", // params: ["Snow", "1"], // game_id: game.id, // user_id: user.id, // system: true // }) // |> Map.from_struct() // assert {:ok, Action { // game: Game { // weather: "Snow" // } // }} = Engine.create_action(action) // } // test "create_action/1 kind: weather, invalid" { // user = insert(:user) // game = insert(:game) // action = build(:action, %{ // kind: "weather", // params: [], // game_id: game.id, // user_id: user.id, // system: true // }) // |> Map.from_struct() // assert {:error, Changeset { // errors: [ // params: {"is invalid", []}, // user_id: {"is not user's turn", []} // ] // }} = Engine.create_action(action) // action = build(:action, %{ // kind: "weather", // params: ["Snow"], // game_id: game.id, // user_id: user.id // }) // |> Map.from_struct() // assert {:error, Changeset { // errors: [ // params: {"must specify duration", []}, // kind: {"must be system action", []} // ] // }} = Engine.create_action(action) // }