Skip to content

Commit

Permalink
chore: added first unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CalliEve committed Jun 20, 2024
1 parent b96b803 commit 9e7b32b
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ node_modules/
package-lock.json
package.json

.vscode/

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
![Check and Deploy](https://img.shields.io/github/check-runs/callieve/metro-map-editor/main?style=flat-square)
![GitHub License](https://img.shields.io/github/license/callieve/metro-map-editor?style=flat-square)
![Website](https://img.shields.io/website?url=https%3A%2F%2Fcalli.dev%2Funiversity%2Fmetro-map&up_message=Online%20editor&up_color=blue&down_message=Temporarily%20unavailable&down_color=red&style=flat-square&label=try%20it%20out!)

[![Try it - yourself!](https://img.shields.io/badge/Try_it-yourself!-blue?style=for-the-badge)](https://calli.dev/university/metro-map)

# Algorithmically Assisted Metro Map Editor

Expand Down
37 changes: 37 additions & 0 deletions exisiting_maps/small_test.graphml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="x" for="node" attr.name="x coordinate" attr.type="int"/>
<key id="y" for="node" attr.name="y coordinate" attr.type="int"/>
<key id="label" for="node" attr.name="station name" attr.type="string"/>
<key id="l0" for="edge" attr.name="lineU1" attr.type="boolean" color.r="84" color.g="167" color.b="33">
<default>FALSE</default>
</key>

<graph id="G" edgedefault="undirected">
<node id="n0">
<data key="x">90</data>
<data key="y">155</data>
<data key="label">test 1</data>
</node>
<node id="n1">
<data key="x">150</data>
<data key="y">126</data>
<data key="label">test 2</data>
</node>
<node id="n2">
<data key="x">210</data>
<data key="y">100</data>
<data key="label">test 3</data>
</node>

<edge id="e0" source="n0" target="n1">
<data key="l0">true</data>
</edge>
<edge id="e1" source="n1" target="n2">
<data key="l0">true</data>
</edge>
</graph>
</graphml>
10 changes: 10 additions & 0 deletions src/models/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,21 @@ impl Line {
self.color = color;
}

/// A getter for the station's color.
pub fn get_color(&self) -> (u8, u8, u8) {
self.color
}

/// A setter for the station's name.
pub fn set_name(&mut self, name: &impl ToString) {
self.name = name.to_string();
}

/// A getter for the station's name.
pub fn get_name(&self) -> &str {
&self.name
}

/// A getter for the station id.
pub fn get_id(&self) -> &str {
&self.id
Expand Down
7 changes: 7 additions & 0 deletions src/models/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ impl Map {
.find(|s| s.get_id() == id)
}

/// Get a [`Line`] with the given id.
pub fn get_line(&self, id: &str) -> Option<&Line> {
self.lines
.iter()
.find(|l| l.get_id() == id)
}

/// Get a mutable [`Line`] with the given id.
pub fn get_mut_line(&mut self, id: &str) -> Option<&mut Line> {
self.lines
Expand Down
27 changes: 27 additions & 0 deletions src/models/station.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ impl Station {
self.name = name.to_string();
}

/// A getter for the name.
pub fn get_name(&self) -> &str {
&self.name
}

/// Clone the [`Station`] without keeping a reference to the coordinate
/// position.
pub fn clone_non_ref(&self) -> Self {
Expand Down Expand Up @@ -126,3 +131,25 @@ impl PartialEq for Station {
other.id == self.id
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_new_station() {
let before_id = STATION_ID.load(Ordering::Relaxed);

let first_station = Station::new((2, 3), None);
let second_station = Station::new((2, 3), Some("test".to_owned()));

let after_id = STATION_ID.load(Ordering::Relaxed);

assert_eq!(before_id + 1, after_id);
assert_eq!(
first_station.get_id(),
before_id.to_string()
);
assert_eq!(second_station.get_id(), "test");
}
}
205 changes: 205 additions & 0 deletions src/utils/graphml/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,208 @@ pub fn graphml_to_map(graph: &GraphMlMap, square_size: u32) -> Map {

map
}

#[cfg(test)]
mod tests {
use super::*;
use crate::utils::graphml::graphml_map::{
Data,
Edge,
Graph,
};

#[test]
fn test_edge_to_line() {
let input = Key {
id: "test".to_owned(),
for_item: "edge".to_owned(),
r: Some("30".to_owned()),
g: Some("0".to_owned()),
b: Some("235".to_owned()),
name: "test line".to_owned(),
};

let result = edge_to_line(&input);

let mut example = Line::new(Vec::new(), &"test");
example.set_color((30, 0, 235));
example.set_name(&"test line");

assert_eq!(result.get_id(), example.get_id());
assert_eq!(result.get_color(), example.get_color());
assert_eq!(result.get_name(), example.get_name());
}

#[test]
fn test_node_to_station() {
let node = Node {
id: "test".to_owned(),
data: vec![
Data {
key: "x".to_owned(),
value: "120.0".to_owned(),
},
Data {
key: "label".to_owned(),
value: "test station".to_owned(),
},
Data {
key: "y".to_owned(),
value: "155".to_owned(),
},
],
};

let result = node_to_station(&node, 30);

let mut example = Station::new((4, 5), Some("test".to_owned()));
example.set_name(&"test station");

assert_eq!(result.get_id(), example.get_id());
assert_eq!(result.get_pos(), example.get_pos());
assert_eq!(result.get_name(), example.get_name());
}

#[test]
fn test_normalize_stations() {
let mut map = Map::new();
map.add_station(Station::new((-3, 4), None));
map.add_station(Station::new((10, 10), None));
map.add_station(Station::new((0, 7), None));

normalize_stations(&mut map);

let edited_stations = map.get_stations();

assert_eq!(edited_stations[0].get_pos(), (2, 2));
assert_eq!(edited_stations[1].get_pos(), (15, 8));
assert_eq!(edited_stations[2].get_pos(), (5, 5));
}

#[test]
fn test_graphml_to_map() {
let graphml = GraphMlMap {
key: vec![
Key {
id: "x".to_owned(),
for_item: "node".to_owned(),
name: "x coordinate".to_owned(),
r: None,
g: None,
b: None,
},
Key {
id: "x".to_owned(),
for_item: "node".to_owned(),
name: "x coordinate".to_owned(),
r: None,
g: None,
b: None,
},
Key {
id: "x".to_owned(),
for_item: "node".to_owned(),
name: "x coordinate".to_owned(),
r: None,
g: None,
b: None,
},
Key {
id: "l0".to_owned(),
for_item: "edge".to_owned(),
name: "lineU1".to_owned(),
r: Some("84".to_owned()),
g: Some("167".to_owned()),
b: Some("33".to_owned()),
},
],
graph: Graph {
content: vec![
GraphItem::Node(Node {
id: "n0".to_owned(),
data: vec![
Data {
key: "x".to_owned(),
value: "90".to_owned(),
},
Data {
key: "label".to_owned(),
value: "test 1".to_owned(),
},
Data {
key: "y".to_owned(),
value: "155".to_owned(),
},
],
}),
GraphItem::Node(Node {
id: "n1".to_owned(),
data: vec![
Data {
key: "x".to_owned(),
value: "150".to_owned(),
},
Data {
key: "label".to_owned(),
value: "test 2".to_owned(),
},
Data {
key: "y".to_owned(),
value: "126".to_owned(),
},
],
}),
GraphItem::Node(Node {
id: "n2".to_owned(),
data: vec![
Data {
key: "x".to_owned(),
value: "210".to_owned(),
},
Data {
key: "label".to_owned(),
value: "test 3".to_owned(),
},
Data {
key: "y".to_owned(),
value: "100".to_owned(),
},
],
}),
GraphItem::Edge(Edge {
id: "e0".to_owned(),
source: "n0".to_owned(),
target: "n1".to_owned(),
data: vec![Data {
key: "l0".to_owned(),
value: "true".to_owned(),
}],
}),
GraphItem::Edge(Edge {
id: "e1".to_owned(),
source: "n1".to_owned(),
target: "n2".to_owned(),
data: vec![Data {
key: "l0".to_owned(),
value: "true".to_owned(),
}],
}),
],
},
};

let map = graphml_to_map(&graphml, 30);

let result_line = map
.get_line("l0")
.expect("no line with id l0");
assert_eq!(result_line.get_color(), (84, 167, 33));
assert_eq!(result_line.get_name(), "lineU1");

let result_station = map
.get_station("n1")
.expect("no station with id n1");
assert_eq!(result_station.get_pos(), (4, 3));
assert_eq!(result_station.get_name(), "test 2");
}
}
2 changes: 1 addition & 1 deletion src/utils/graphml/graphml_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(super) struct Key {
pub(super) r: Option<String>,
#[serde(rename = "@color.g")]
pub(super) g: Option<String>,
#[serde(rename = "@color.g")]
#[serde(rename = "@color.b")]
pub(super) b: Option<String>,
#[serde(rename = "@attr.name")]
pub(super) name: String,
Expand Down
25 changes: 25 additions & 0 deletions src/utils/graphml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,28 @@ pub fn decode_map(input: &str, square_size: u32) -> Result<Map, DeError> {

Ok(graphml_to_map(&decoded, square_size))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_decode_map() {
let test_file_content = std::fs::read_to_string("exisiting_maps/small_test.graphml")
.expect("test data file does not exist");

let result = decode_map(&test_file_content, 30).expect("failed to decode graphml");

let result_line = result
.get_line("l0")
.expect("no line with id l0");
assert_eq!(result_line.get_color(), (84, 167, 33));
assert_eq!(result_line.get_name(), "lineU1");

let result_station = result
.get_station("n1")
.expect("no station with id n1");
assert_eq!(result_station.get_pos(), (4, 3));
assert_eq!(result_station.get_name(), "test 2");
}
}
Loading

0 comments on commit 9e7b32b

Please sign in to comment.