Commit 7bc51078 authored by Laura Schlimmer's avatar Laura Schlimmer
Browse files

small util function fixes

parent d62bbd5f
Loading
Loading
Loading
Loading
+61 −1
Original line number Diff line number Diff line
@@ -153,6 +153,7 @@ FnordMetric.UnitTests = function() {

  var testGenerateSQLQueryFromParams = function() {
    function test(params, expected) {
      results.total++;
      var result = FnordMetric.util.generateSQLQueryFromParams(params);
      result = result.replace(/(\n\r|\n|\r|\s)/gm, "");
      if (result != expected) {
@@ -212,11 +213,70 @@ FnordMetric.UnitTests = function() {

  }();

  var testAddToCSV = function() {
    function test(list, value, expected) {
      results.total++;
      var result = FnordMetric.util.addToCSV(list, value);
      if (result != expected) {
        results.bad++;
        console.log("Testing addToCSV expected "
          + expected + ", but was " + result);
      }
    }
    test("", "host", "host");
    test("host", "mount_name", "host,mount_name");
    test("mount_name", "", "mount_name");
  }();

  var testRemoveFromCSV = function() {
    function test(list, value, expected) {
      results.total++;
      var result = FnordMetric.util.removeFromCSV(list, value);
      if (result != expected) {
        results.bad++;
        console.log("Testing removeFromCSV expected " +
          expected + ",but was " + result);
      }
    }
    test("host,mount_name", "mount_name", "host");
    test("", "host", "");
    test("host,mount_name", "provider", "host,mount_name");
    test("host,mount_name", "", "host,mount_name");
  }();

  var testMakeLowerCaseUnderscore = function() {
    function test(string, expected) {
      results.total++;
      var result = FnordMetric.util.makeLowerCaseUnderscore(string);
      if (result != expected) {
        results.bad++;
        console.log("Testing makeLowerCaseUnderscore expected " +
          expected + ",but was " + result);
      }
    }
    test("host name", "host_name");
    test("hostname", "hostname");
    test("HoSt  Name", "host_name");
  }();

  var testReverseLowerCaseUnderscore = function() {
    function test(string, expected) {
      results.total++;
      var result = FnordMetric.util.reverseLowerCaseUnderscore(string);
      if (result != expected) {
        results.bad++;
        console.log("Testing reverseLowerCaseUnderscore expected " +
          expected + ",but was " + result);
      }
    }
    test("host_name", "Host Name");
    test("hostname", "Hostname");
  }();

  if (results.bad == 0) {
    console.log("Yeah, all " + results.total + " tests passed :-) ");
  } else {
    console.log(results.bad + " tests of " + results.totak + " failed.");
    console.log(results.bad + " tests of " + results.total + " failed.");
  }

}
+5 −1
Original line number Diff line number Diff line
@@ -610,7 +610,7 @@ FnordMetric.util.getDateTimeString = function(timestamp) {
}

FnordMetric.util.makeLowerCaseUnderscore = function(string) {
  return (string.toLowerCase().replace(/ /g,"_"));
  return (string.toLowerCase().replace(/\s\s|\s/g,"_"));
}

FnordMetric.util.reverseLowerCaseUnderscore = function(string) {
@@ -626,10 +626,14 @@ FnordMetric.util.reverseLowerCaseUnderscore = function(string) {
  return str;
}

/* doesn't check if value is already in list */
FnordMetric.util.addToCSV = function(list, value) {
  if (list.length == 0) {
    return value;
  }
  if (value.length == 0) {
    return list;
  }
  var values = list.split(",");
  values.push(value);
  list = values.join(",");