Some initial tests

main
Michał Rudowicz 2022-05-14 13:58:38 +02:00
rodzic d67f935022
commit f941f87756
6 zmienionych plików z 205 dodań i 3 usunięć

Wyświetl plik

@ -10,7 +10,7 @@ namespace Hamlocator {
this.l2m = "abcdefghijklmnopqrstuvwxyz";
}
public string get_current_location (GClue.Location pos) {
public string get_locator(GClue.Location pos) {
double lat = pos.latitude + 90;
double lon = pos.longitude + 180;

Wyświetl plik

@ -1,11 +1,17 @@
hamlocator_sources = [
'main.vala',
'window.vala',
'application.vala',
'location.vala',
'maidenhead.vala'
]
test_sources = [
'tests' / 'main.vala',
'tests' / 'testcase.vala',
'tests' / 'maidenhead_tests.vala',
'tests' / 'fake_geoclue_location.vala'
]
hamlocator_deps = [
dependency('libadwaita-1', version: '>= 1.0'),
dependency('libgeoclue-2.0'),
@ -18,9 +24,16 @@ hamlocator_sources += gnome.compile_resources('hamlocator-resources',
c_name: 'hamlocator'
)
executable('hamlocator', hamlocator_sources,
executable('hamlocator', [hamlocator_sources, 'main.vala'],
vala_args: '--target-glib=2.50 --pkg=gio-2.0',
dependencies: hamlocator_deps,
link_args: '-lm',
install: true,
)
test('ut', executable('hamlocator-tests', [hamlocator_sources, test_sources],
vala_args: '--target-glib=2.50 --pkg=gio-2.0',
dependencies: hamlocator_deps,
link_args: '-lm',
install: true,
))

Wyświetl plik

@ -0,0 +1,42 @@
public class FakeGeoclueLocation : GLib.Object, GClue.Location {
private double lat;
private double lon;
public FakeGeoclueLocation(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}
public override double accuracy {
get { return double.NAN; }
set { assert(false); }
}
public override double altitude {
get { return double.NAN; }
set { assert(false); }
}
public override string description { owned
get { return ""; }
set { assert(false); }
}
public override double heading {
get { return double.NAN; }
set { assert(false); }
}
public override double latitude {
get { return this.lat; }
set { assert(false); }
}
public override double longitude {
get { return this.lon; }
set { assert(false); }
}
public override double speed {
get { return double.NAN; }
set { assert(false); }
}
public override Variant timestamp {
owned get { return ""; }
set { assert(false); }
}
}

Wyświetl plik

@ -0,0 +1,34 @@
public class MaidenheadTests : TestCase {
public MaidenheadTests() {
base("Maidenhead Tests");
add_test_location(0.0, 0.0, "JJ00aa");
add_test_location(90.0, 0.0, "JS00aa");
add_test_location(0.0, 180.0, "SJ00aa");
add_test_location(90.0, 180.0, "SS00aa");
add_test_location(-90.0, 0.0, "JA00aa");
add_test_location(0.0, -180.0, "AJ00aa");
add_test_location(-90.0, -180.0, "AA00aa");
add_test_location(90.0, -180.0, "AS00aa");
add_test_location(-90.0, 180.0, "SA00aa");
add_test_location(51.1205, 17.0261, "JO81mc");
add_test_location(37.1104, -5.4932, "IM77gc");
add_test_location(-30.5377, 22.8516, "KF19kl");
add_test_location(-27.4613, -65.0391, "FG72lm");
add_test_location(-24.6168, 136.4063, "PG85ej");
}
protected Hamlocator.LocationToMaidenhead tested;
public override void set_up() {
this.tested = new Hamlocator.LocationToMaidenhead();
}
public void add_test_location(double lat, double lon, string expected) {
add_test (@"Expect location lat:$lat lon:$lon to be $expected", () => {
GClue.Location pos = new FakeGeoclueLocation(lat, lon);
assert_cmpstr(this.tested.get_locator(pos), EQ, expected);
});
}
}

Wyświetl plik

@ -0,0 +1,30 @@
/* main.vala
*
* Copyright (C) 2009 Julien Peeters
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Julien Peeters <contact@julienpeeters.fr>
*/
void main(string[] args) {
Test.init(ref args);
TestSuite.get_root().add_suite(new MaidenheadTests().get_suite());
Test.run();
}

Wyświetl plik

@ -0,0 +1,83 @@
/* testcase.vala
*
* Copyright (C) 2009 Julien Peeters
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Julien Peeters <contact@julienpeeters.fr>
*/
// Based on https://gitlab.gnome.org/GNOME/libgee/-/raw/main/tests/testcase.vala
public abstract class TestCase : Object {
private GLib.TestSuite suite;
private Adaptor[] adaptors = new Adaptor[0];
public delegate void TestMethod ();
protected TestCase (string name) {
this.suite = new GLib.TestSuite (name);
}
public void add_test (string name, owned TestMethod test) {
var adaptor = new Adaptor (name, (owned)test, this);
this.adaptors += adaptor;
this.suite.add (new GLib.TestCase (adaptor.name,
adaptor.set_up,
adaptor.run,
adaptor.tear_down ));
}
public virtual void set_up () {
}
public virtual void tear_down () {
}
public GLib.TestSuite get_suite () {
return this.suite;
}
private class Adaptor {
[CCode (notify = false)]
public string name { get; private set; }
private TestMethod test;
private TestCase test_case;
public Adaptor (string name,
owned TestMethod test,
TestCase test_case) {
this.name = name;
this.test = (owned)test;
this.test_case = test_case;
}
public void set_up (void* fixture) {
this.test_case.set_up ();
}
public void run (void* fixture) {
this.test ();
}
public void tear_down (void* fixture) {
this.test_case.tear_down ();
}
}
}